Here's the links for GraphQL Website and its Github Repositories
When building an application using GraphQL, There are few new terminologies we will come across, Some of them are
Type
Query
Mutation
Resolve
Subscription
Schema
Let's dig deeper into each of these terms and understand what it means.
Type
is the type of Object/Field we're creating or accessing.If you have used databases you must already be familiar with this as we have different types for fields in Databases like String, Integer, Float, Number, Varchar, Text, etc.
It's just like that, Here we have different types which we can associate with fields.
In GraphQL We have to associate every field with a Type.
MongoDB _id
fieldRequired
.There are many more field Types, You can find the full list here http://graphql.org/learn/schema/.
Along with the existing Types, We can very easily create our own Field types, This can be useful for Email, URL, Date, Phone, Address, etc fields.
We can specify as many queries as we want in our GraphQL Application and we can call those queries as needed.,
We can pass optional
or required
parameters to the query as well, And we can use those parameters internally to filter and return the correct data.
Anyways, Query doesn't fetch the data, It acts as a proxy, It processes the incoming GraphQL request and calls the resolve
method and passes along all the parameters, Its the job of resolve method to fetch the data from any source and return it back to the query.
The query then checks the data for correct field types and returns it back to the client.
Everytime we want to add/manipulate a record, We call GraphQL Mutation and pass all the arguements/parameters., The mutation then calls the resolve
method and passes along all the data it receives.
Here we do wherever we want with the data and however we want to do it, once done we return the response back to the mutation, which in turn will return the response back to the client. (after validating its field types)
We can fetch the data from Database, API, File, or any datasource we can imagine/have access to, GraphQL doesn't restrict us.
In resolve method we can easily return a Promise
which makes our code non-blocking, is good practice and gives us more freedom on data fetching and manipulation.
Once we have the data, We return it back to the handler., Which then validates the data, its type and structure.
The field type must match the type we specify while creating a Record Type
otherwise error will be thrown. (lot of types here).
After successful validation, The data is returned back to the client.
We have to specify on our own when the data changes, Which query its associated with and which and all clients to update.
This is relatively new, But a step forward in the direction of real time updates.
Here we create a RootQuery
(name can be anything) and here we specify all the queries we have written.
Similarly, We create a RootMutation
and specify all the mutations we have written.
Finally we return a GraphQLSchema
with this query
and mutation
object.
The server we create will make use of this GraphQLSchema object and handle things from there.
I myself was sceptical of GraphQL, But this Udemy course by Stephen Grider piqued my interest.
I developed an Open Source application using GraphQL and the experienc was great.
You can checkout the Application here https://github.com/dhruv-kumar-jha/productivity-frontend.
In upcomig tutorials we will setup our Express server, MongoDB and Write our first Type
, Query
and Mutation
. And then the app (which i still haven't decided what it will be.)
If you have any questiosn/queries/feedback, Feel free share them here, on twitter or by contacting me.