GraphQL - Schema (Type)

About

This article is about the schema (type) definition of GraphQL.

The implementation graphql/type module is the engine for defining GraphQL types and schema.

Example

The below type is a shorthand notation. The API is more expressive and allows types and fields to be documented. See the getting started schema starWarsSchema.js

enum Episode { NEWHOPE, EMPIRE, JEDI }
interface Character {
  id: String!  // ! means not null
  name: String
  friends: [Character]
  appearsIn: [Episode]
}
  • types implementing ther interface
type Human implements Character {
  id: String
  name: String
  friends: [Character]
  appearsIn: [Episode]
  homePlanet: String
}

type Droid implements Character {
  id: String
  name: String
  friends: [Character]
  appearsIn: [Episode]
  primaryFunction: String
}
  • The Entry point that defines the whole schema (by default, the name is Query)
type Query {
  hero(episode: Episode): Character
  human(id: String!): Human
  droid(id: String!): Droid
}

Built-in

  • String, Boolean - Are built-in scalars that the type system provided.
  • __Schema, __Type, __TypeKind, __Field, __InputValue, __EnumValue, __Directive preceded with a double underscore, indicating that they are part of the introspection system.





Discover More
GraphQL - Schema Introspection

Schema Introspection (ie querying the Schema and not the data) Example: graphql/graphql-js/blob/master/src/__tests__/starWarsIntrospection-test.js The graphql/graphql-js/blob/master/src/type/introspection.jsintrospection...
What is GraphQL (Graph Query Language)?

is a graph-based, schema-based, SQL-like query language where queries returns a tree of data (json objects) that should match a front-end view, regardless of where that data was pulled from....



Share this page:
Follow us:
Task Runner