Graphql datatypes: Difference between revisions

From wikinotes
No edit summary
Line 37: Line 37:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Lists -->
</blockquote><!-- Lists -->
= Interfaces =
<blockquote>
In your schema (note you can implement multiple interfaces)
<syntaxhighlight lang="python">
# schema
interface Animal {
  species: String!
  age:    Int!
}
type Duck implements Animal {
  species: String!
  age:    Int!
  quack:  String!
}
</syntaxhighlight>
You can return an <code>Animal</code> (which really returns any of the subtypes).<br>
When referring to fields not part of the return type, you need to specify the object it belongs to.
<syntaxhighlight lang="python">
{
  animal {
    species
    age
    ... on Duck {
      quack
    }
  }
}
</syntaxhighlight>
</blockquote><!-- Interfaces -->

Revision as of 03:18, 3 September 2021

Documentation

type docs (scroll down) https://graphql.org/learn/schema/

Primitives

String
Int
Float
Boolean
ID         # see docs

Enums

enum PrimaryColor {
  RED
  BLUE
  YELLOW
}

Lists

[String]   # homogenous typed list
[String!]  # list can be null, members must be strings (not null)

Interfaces

In your schema (note you can implement multiple interfaces)

# schema
interface Animal {
  species: String!
  age:     Int!
}

type Duck implements Animal {
  species: String!
  age:     Int!
  quack:   String!
}

You can return an Animal (which really returns any of the subtypes).
When referring to fields not part of the return type, you need to specify the object it belongs to.

{
  animal {
    species
    age
    ... on Duck {
      quack
    }
  }
}