GraphQL — Beginner’s Part

Krina Soni
4 min readAug 31, 2019

GraphQL is a query language for your API, and a server-side run-time for executing queries by using a type system you define for your data.

Why should we use it ?
It isn’t bind to any specific database or storage engine. You can easily use it for your existing or new application with any kind of database.

There are also few problems with REST-full API solved by GraphQL.
1. REST-full nested routing
2. Over fetching data while nesting routes and queries.
When there is nesting of routes & multiple requests/queries are getting executed, you will get all data that is useful or useless. GraphQL will respond with data you need.
3. Multiple REST requests
Using GraphQL, you can get all the data your application needs in a single request.

GraphQL Types

Character — Which will be used as GraphQL Object Type.
String — Used for single scalar object which can not have any sub-selections in the query.
In the type language when a field is mandatory and non null-able, It can be represented using exclamation mark.
It will be like String!
To represent array of Objects, [User] and to make it as non null-able, It will look like [User]!

Scalar Types

GraphQL comes with few default scalar types : Boolean, Int, String, Float, ID — which is used for unique identification.
User can also specify custom scalar type.

scalar Date

This will define scalar type as Date and You can use in schema as one of its type.

Every GraphQL service has a query type and it may or may not have Mutation type. While query type is entry point for every graphql query.

Also, When we are talking about building an API, there will be errors and in REST API there are few ways for Error management.
In GraphQL, errors will be listed down along with the success data. In case of server internal errors, It will not form a response and instead held it for query to process and throw any error.

GraphQL Query provides three params for the response of query execution.
loading, error, data.

Let’s see what it takes to build a functional application using GraphQL.

Queries, Mutations & Subscriptions.

Queries

Query is used to make a request to server to fetch data that you need. You can ask for specific fields, to pass arguments in nested queries to fetch objects or fields you required.

type Query {
Users:{
id,
name,
email
}
}`;

This Query will fetch Users Object with id, name, and email fields.
You may pass arguments as if you want to fetch user details based on ID.

{ 
Users (id: 3)
{
id
name
email
}
}

Basically Query is used for fetch data as we do with GET method in REST-full API.

Mutations

We have fetched data of all users and also through passing arguments. You will also need to write data to database server by adding or updating it. You can do all this by Mutation.

Let’s take a tour for How to make a application server which gets requests and do some queries on it and provides response using express.

First, We’ll create a directory with name of a application and initialize it with few dependencies and add src folder to it.
npm install apollo-server-express graphql graphql-tools express --save

To run a server, We’ll create server file to project directory.

We have created ApolloServer with schema and resolvers. (We’ll get to the schema & resolvers in a while) Also we have enabled playground to true.

GraphiQL is a GraphQL IDE which provides you interactive user interface to run your query.

Also, We have created express server and added the express server to GraphQL middleware.
Now, Let’s go to the Schema and Resolver.

Schema

Every GraphQL service defines a set of types which completely describe the set of possible data you can query on that service. Then, when queries come in, they are validated and executed against that schema.

This is what official documentation states which we basically can relate to the database schema that we make usually in daily routine.

Resolver

When using graphql-tools, you define your field resolvers separately from the schema. Since the schema already describes all of the fields, arguments, and result types, the only thing left is a collection of functions that are called to actually execute these fields.

Resolvers basically return a Promise and which are used for fetching API data from database or to call REST API.

A resolver function have four arguments ,

i. obj — The previous object used on root Query.
ii. args — The arguments provided to the field in the GraphQL query.
iii. context — A value which is provided to every resolver and holds important contextual information like the currently logged in user, or access to a database.
iv. info — A value which holds field-specific information relevant to the current query as well as the schema details.

A simple example of query using apollo-server-express, graphql and express. I have connected MongoDB and used mongoose for the same.

Here is Github link for the project. graphql-demo.

--

--

Krina Soni

Passionate about places, photos and coding! Food is love !!