GraphQL is a data query and manipulation language developed by Facebook. With GraphQL, you get precisely the data you request. No more, no less. You have a single entry point, i.e., /graphql
, that you use to get the data.
GraphQL schema is the blueprint of a GraphQl API. It describes what data we can query from the API and how the API will return the data.
You define a schema that describes all the possible data you may need to query in your application. The schema also specifies the queries and mutations your applications can use to read or update data on the server.
It all starts from types. The graphQL schema uses the types and interactions between them. You define an object type first and add fields of different types in it like this:
graphql
type Address {street: Stringcode: Int!location: Geo}type Geo {longitude: int!latitude: int!}
Here Address
is an object type with fields of street
, code
, and location
. The exclamation mark (!) such as Int!
marks the field as mandatory, which means it can't be null. An object type can also have another object as its field. Like in the above example, the location field has the Geo
object type.
GraphQL Schemas use the Schema Definition Language (SDL), which defines the following types:
The built-in scalar types in GraphQL include ID, Float, Int, String, and Boolean.
As discussed in the example above, these are custom types that consist of their own fields (properties). Each field can have any of the available types.
It is a special object type that provides the entry points for making queries. It also defines the data returned by the query.
Consider the following example query type:
graphql
type Query {posts: [Post]users: [User]}
This type allows the client to query posts and users, like this:
graphql
query GetPosts {posts {title}}
It is similar to the query type, but it provides the entry points for creating or updating data. It also defines the data returned by the mutation. For example:
graphql
type Mutation {addPost(title: String!, body: String!): Post}
Clients can use this mutation like this:
graphql
mutation CreatePost {addBook(title: "This is a new post", body: "lorem ipsum") {title}}
The above mutation will create a new post and return its title.
This object type allows clients to fetch data from the server in real-time. Read more about subscriptions in this interactive guide.
Enum types behave like scalar types but with a restricted list of options. For example:
graphql
enum SortOrder {ASCENDINGDESCENDING}
Now, we can use this enum in other object types, and it will only accept ASCENDING
or DESCENDING
as its value.
These types allow the client to pass arguments in queries or mutations.
graphql
input UpdatePostInput {title: Stringbody: String}
Using it to change our example in the mutation type has the following effect:
graphql
# Without Input Typetype Mutation {addPost(title: String!, body: String!): Post}# With Input Typetype Mutation {addPost(content: UpdatePostInput!): Post}
Finally, unions and interfaces are abstract types that allow a field to have multiple object types.