Topics In Demand
Notification
New

No notification found.

Blog
GraphQL Basics – Part 1

June 4, 2019

656

0

This is the first blog of the GraphQL series that explains the basics of how GraphQL works.

What is GraphQL?

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

Advantages of using GraphQL over REST APIs

One of the most common problems with REST is that of over and under fetching of data. These happen because the only way for a client to download data is by hitting endpoints that return fixed data structures. It’s very difficult to design the API in a way that it’s able to provide clients with their exact data needs.

  • GraphQL reduces network requests by allowing us to fetch or retrieve all the data we need in a single query.
  • With GraphQL, there is no need for versioning as we can easily add new fields and types to our GraphQL API without impacting existing queries. Also, we can easily mark fields as deprecated, and the fields will be excluded from the response received from the server.
  • With GraphQL, you can also do low-level performance monitoring of the requests that are processed by your server. GraphQL uses the concept of resolver functions to collect the data that’s requested by a client. Instrumenting and measuring performance of these resolvers provides crucial insights about bottlenecks in your system.

Brief Explanation of How GraphQL works

GraphQL implementation mainly contains three parts

  • Schema – Describes the data
  • Resolvers – Logic for fetching data from different resources (microservices)
  • Query – Client asks for what data to be fetched

Schema (More about Schema and types)

P.S: “!” means that the field cannot be null or undefined.The above code describes what can be queried and response data (data type) expected from the resources.

Resolvers (Below code is written in NodeJs)

The above code describes the logic that gets executed when the client requests.

P.S. I have used full_name resolver function for User type for writing my own logic.

Query (More about query)

The client queries using above code which returns only full_name and pic as below.

P.S. Client can only query for fields which are defined in the Schema.

The client request can be of the types Query and Mutation ( and Subscription. This will be discussed in future blogs of the GraphQL series).

Both requests do the same. Except that mutation is executed synchronously. The convention that is followed is Mutation is used for any operation that causes writes in the server ( update profile, create order etc. ) and a query is used for fetching data (get menu, get vouchers list, etc.). ( Similar to GET and POST ). Technically both types of these requests can be used to perform any logic. But it is better to follow convention as it helps your team.

Important Points for an understanding relationship between resolver functions and query

  • You can think of each field in a GraphQL query as a function or method of the previous type which returns the next type. In fact, this is exactly how GraphQL works. Each field on each type is backed by a function called the resolver which is provided by the GraphQL server developer. When a field is executed, the corresponding resolver is called to produce the next value.
  • If a field produces a scalar value like a string or number, then the execution completes. However, if a field produces an object value, then the query will contain another selection of fields which apply to that object. This continues until scalar values are reached. GraphQL queries always end at scalar values.

A resolver function receives four arguments:

Syntax

  • obj The previous object, which for a field on the root Query type is often not used.
    For understanding more about this argument refer to this link
  • args The arguments provided to the field in the GraphQL query. In the above resolver function {user_input} is the args
  • 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. In the above resolver, function context is used for passing token_info
  • info A value which holds field-specific information relevant to the current query as well as the schema details. Often not used

Clone the demo GraphQL project from here https://github.com/giftxoxodev/GraphQL.git

P.S. In the next part of the GraphQL series, I will discuss how to implement GraphQL as an API Gateway.

About the Author

Sai’s passion for web development is only trumped by his passion for food.
In his spare time, Sai utilizes his creative talents building web games, experimenting with digital art and watches Netflix.


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


Xoxoday

© Copyright nasscom. All Rights Reserved.