in the previous article, we learned about GraphQL and how much GraphQl changes the way we integrate the systems and exchange the data; in this article, we will talk about some fundamental language constructs of GraphQL such as the GraphQL schema, queries, mutations, and subscriptions.GraphQL has its own type system that’s used to define the schema of an API , the Syntax of writing schema is calling SDL(schema definition language ) ,GraphQL services can be written in any language. Since we can’t rely on a specific programming language syntax, like c#, We’ll use the “GraphQL schema language” – it’s similar to the query language, and allows us to talk about GraphQL schemas
Object types
object type its represent the entity of your project like person , school Blog,etc the below examples show you how we define tow objects types one is called blog and other is called author
type Author{
name :string!
age :int!
}
type Post{
name :String!
createDate :int!
description :String
}
note that The exclamation point following at the field type means that this field is required. It’s also possible to create relationships between GraphQL types. In GraphQL that’s simply called a relation. to add a relation between the Author and the Post types to express that one Author can have many posts we just need to ad the author field in the post type like the below
type Post{
name :String!
createDate :int!
description :String
auther :Auther!
}
then we need add the post field in the Author type to express that the person can have multiple post and this can be like below , uses square brackets to specify that post is a list as many other programming languages
type Author{
Name :string!
age :int!
posts :[Post!]!
}
Arguments
Every field on a GraphQL object type can have zero or more arguments and to work with argument you have to conseder the following :
- All arguments are named. Unlike some languages like JavaScript and Python where functions take a list of ordered arguments,
- all arguments in GraphQL are passed by name specifically
- Arguments can be required or optional. When an argument is optional, you can define a default value
for Example the All post hav an optional argument wich is rowcount , so the client did not give it a value it will take the default value wich is 10
type AllPost (rowcount:10){
Name
age
posts
}
Mutations
most of our application need to do changes to the data that currently stored in our backend . these changes can be done by using the mutations , its follows the same structure of the query but always need to start with the mutation keyword ; mutations divide into three types:
- Creating New data
- Updating data
- Deleting data
type Mutation {
createPost(name: String , description string): post
}
as you noticed the mutation also has a root field (createPost). We also learned about the concept of arguments for fields. In this case the createPost field takes two arguments that specify the new Post name and description.
Scalar types
GraphQL comes with a set of default scalar types out of the box:
Int
: A signed 32‐bit integer.Float
: A signed double-precision floating-point value.String
: A UTF‐8 character sequence.Boolean
:true
orfalse
.ID
: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as anID
signifies that it is not intended to be human‐readable.
In most GraphQL service implementations, there is also a way to specify custom scalar types. For example, we could define a Date
type:
Enumeration types
Also called Enum, these types are a special kind of scalar that is restricted to a set of values. This allows you to:
enum Gender{
Male
FeMale
Other
}
Interfaces
Interface is an abstract type that includes a certain set of fields that a type must include to implement the interface.
interface Blog {
id: ID!
name: String!
posts: [post]
Aother: [Aother]!
}
at the end these are some of the graphQL Main schema types you can find another type like union types and Query types at the graphQL document page also you can see the references below ;
https://www.howtographql.com/graphql-js/3-a-simple-mutation/