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 application1234567891011121314<span class="token keyword">type</span> <span class="token class-name">Post</span> <span class="token punctuation">{</span><span class="token attr-name">id</span> <span class="token punctuation">:</span> String <span class="token operator">!</span><span class="token attr-name">title</span> <span class="token punctuation">:</span> String <span class="token operator">!</span><span class="token attr-name">publishedAt</span> <span class="token punctuation">:</span> DateTime <span class="token operator">!</span><span class="token attr-name">likes</span> <span class="token punctuation">:</span> Int <span class="token operator">!</span> <span class="token directive function">@default</span> <span class="token punctuation">(</span> <span class="token attr-name">value</span> <span class="token punctuation">:</span> <span class="token number">0</span> <span class="token punctuation">)</span><span class="token attr-name">blog</span> <span class="token punctuation">:</span> Blog <span class="token directive function">@relation</span> <span class="token punctuation">(</span> <span class="token attr-name">name</span> <span class="token punctuation">:</span> <span class="token string">"Posts"</span> <span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token keyword">type</span> <span class="token class-name">Blog</span> <span class="token punctuation">{</span><span class="token attr-name">id</span> <span class="token punctuation">:</span> String <span class="token operator">!</span><span class="token attr-name">name</span> <span class="token punctuation">:</span> String <span class="token operator">!</span><span class="token attr-name">description</span> <span class="token punctuation">:</span> String<span class="token attr-name">posts</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span> Post <span class="token operator">!</span> <span class="token punctuation">]</span> <span class="token operator">!</span> <span class="token directive function">@relation</span> <span class="token punctuation">(</span> <span class="token attr-name">name</span> <span class="token punctuation">:</span> <span class="token string">"Posts"</span> <span class="token punctuation">)</span><span class="token punctuation">}</span>
- 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 example12345<span class="token keyword">type</span> <span class="token class-name">Character</span> <span class="token punctuation">{</span><span class="token attr-name">name</span> <span class="token punctuation">:</span> String <span class="token operator">!</span><span class="token attr-name">appearsIn</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span> Episode <span class="token operator">!</span> <span class="token punctuation">]</span> <span class="token operator">!</span><span class="token punctuation">}</span>
Character
is a GraphQL Object Type , which is an object type with fields . Most of the types in your schema are object typesname
andappearsIn
areCharacter
fields . This means thatname
andappearsIn
are the only fields specified when querying forCharacter
- 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 ofEpisode
.
3. Arguments
- Each field of an object type can have no or more arguments. For example, the
lenght
field below:123456<span class="token keyword">type</span> <span class="token class-name">Starship</span> <span class="token punctuation">{</span><span class="token attr-name">id</span> <span class="token punctuation">:</span> ID <span class="token operator">!</span><span class="token attr-name">name</span> <span class="token punctuation">:</span> String <span class="token operator">!</span><span class="token attr-name">length</span> <span class="token punctuation">(</span> <span class="token attr-name">unit</span> <span class="token punctuation">:</span> LengthUnit <span class="token operator">=</span> <span class="token constant">METER</span> <span class="token punctuation">)</span> <span class="token punctuation">:</span> Float<span class="token punctuation">}</span> - 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 asunit
- 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 ofMETER
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
type12<span class="token keyword">scalar</span> <span class="token class-name">Date</span>
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 example123456<span class="token keyword">enum</span> <span class="token class-name">Episode</span> <span class="token punctuation">{</span><span class="token constant">NEWHOPE</span><span class="token constant">EMPIRE</span><span class="token constant">JEDI</span><span class="token punctuation">}</span>
For any field withEpisode
type, it can only beNEWHOPE
,EMPIRE
, orJEDI
6. Epilogue 1
- In this article, I have presented some of the basic and most commonly used concepts when defining a GraphQL Schema. Part 2 I will present more complex concepts, but also very important and useful
- Reference source