GraphQL SDL: Introduction to Schema Definition Language #3

GraphQL has its own language to write GraphQL Schemas: The GraphQL Schema Definition Language (SDL)


In this post, you'll learn all you need to know about the GraphQL type system and how it describes what data can be queried.

Since GraphQL can be used with any backend framework or programming language, we'll stay away from implementation-specific details and talk only about the concepts.

Image Credits: https://www.graph.cool/
Image Credits: https://www.graph.cool/

GraphQL Type language

GraphQL services can be written in any language. Since we can't rely on a specific programming language syntax, like JavaScript, to talk about GraphQL schemas, we'll define our own simple language.

We'll use the "GraphQL schema language" - it's similar to the query language and allows us to talk about GraphQL schemas in a language-agnostic way
. . .


GraphQL Schema Definition

A GraphQL Schema Definition is the most concise way to specify a GraphQL schema. The syntax is well-defined and is part of the official GraphQL specification.

Schema Definitions are sometimes referred to as IDL (Interface Definition Language) or SDL (Schema Definition Language).

The GraphQL schema for a star-wars app could be specified like this:
type Character { name: String! appearsIn: [Episode!]! }

enum Episode { NEWHOPE EMPIRE JEDI }

Don't worry about the above types, we will look into all the graphql schema types in next blog.
. . .

GraphQL Type system

If you've seen a GraphQL query before, you know that the GraphQL query language is basically about selecting fields on objects. So, for example, in the following query:
#GraphQL Query
{ hero { name appearsIn } }
#Output { "data": { "hero": { "name": "R2-D2", "appearsIn": [ "NEWHOPE", "EMPIRE", "JEDI" ] }
} }
  1. We start with a special "root" object
  2. We select the hero field on that
  3. For the object returned by hero, we select the name and appearsIn fields

Because the shape of a GraphQL query closely matches the result, you can predict what the query will return without knowing that much about the server. But it's useful to have an exact description of the data we can ask for - what fields can we select? What kinds of objects might they return? What fields are available on those sub-objects? That's where the schema comes in.

Every GraphQL service defines a set of types that completely describe the set of possible data you can query on that service. Then, when queries come in, they are validated and executed against that schema.
. . .
In the next blog, we will look into the GraphQL scalar types, enums & Object types and fields.



Never miss a post from Gufran Mirza, when you sign up for Ednsquare.