GraphQL SDL – Schema Definition Language Part 1

Tram Ho

1. What is GraphQL Schema Difinition Language?

  • Through previous articles in the series about GraphQL , we have understood the concepts of GraphQL, Schema. GraphQL has a separate language for defining Schema which is GraphQL Schema Definition Language ( SDL ).
  • SDL is a language with a very simple, easy to understand, powerful and intuitive syntax that makes the schema definition the most concise.
  • The example uses SDL to define the schema for a simple blogging application
  • The main elements of the schema definition include types and fields . Other additional information that may be provided are custom directives like default value specified for the likes field or relation specifying the relationship … There’s a lot to say here, let’s go Go into detail.

2. Object types and fields

  • The most basic element of GrapQL Schema is object types , which represent a kind of object that you can get from the server.
  • For example
  • Character is a GraphQL Object Type , which is an object type with fields . Most of the types in your schema are object types
  • name and appearsIn are Character fields . This means that name and appearsIn are the only fields specified when querying for Character
  • A field is composed of name and type.
  • The string here is the type of the field name , which is a built-in scalar type . We will learn about scalar types below.
  • An external field of type scalar type can be of any type defined in the schema.
  • A field that cannot be null is indicated by a sign ! , the server is required to return data for this field when you query.
  • A field of type is an array specified with [] , where appearsIn is an array of Episode .

3. Arguments

  • Each field of an object type can have no or more arguments. For example, the lenght field below:
  • Unlike other programming languages ​​like JavaScript or Python, functions will take the parameters in the order when defining the functon, in GraphQL, the fields will take the parameters according to the name passed, the order in which the parameters are not affect. In the example above the length field has one parameter defined as unit
  • An argument may or may not be required. When a parameter is not required, we can define a default value for it, if the unit parameter is not passed, it will have the default value of METER

4. Scalar types

  • Scalar type is the type for a scalar object, which means that this object has no sub-selections, they are considered leaves of the query.
  • There are 5 types of Scalar types defined in the SDL
    • Int : An unsigned 32-bit integer
    • Float : A floating-point real number
    • String : A UTF-8 string
    • Boolean : true or false
    • ID : is a unique identifier string, usually used to retrieve an object from the server
  • In addition to the predefined scalar types , we can define custom scalar types ourselves. For example we can define a Date type

    For the type of Date we can validate or format it as Y / mm / dd, so any Date field returned by the server will be of the form Y / mm / dd

5. Enumeration types

  • Enums are also a special type of scalar type, limited to a specific allow value, we can validate any argument of this type that is only valid within the allow values.
  • For example

    For any field with Episode type, it can only be NEWHOPE , EMPIRE , or JEDI

6. Epilogue 1

Share the news now

Source : Viblo