Getting started with GraphQL

Reading Time: 2 minutes

The world is a stage where all of us are artists. Constant learning is the foundation of success. So, here we are going to learn about a query language introduced by Facebook back in 2015, which is GraphQL. In this blog, we will cover the basics of GraphQL.

Overview

GraphQL is a query language(that’s what QL stands for) for your API and a server-side runtime for executing queries. Basically, it is used to load data from a server to a client(it’s a way to get data from an API into your application). GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data.

Advantages over REST

  • You can easily get data by accessing only ONE endpoint in GraphQL. As in REST, you had to gather the data by accessing multiple endpoints.
  • We deal with over- and under-fetching in REST(A client can only download data by hitting endpoints that return fixed data structures). But in GraphQL the client can specify exactly the data he/she needs.

There are many more advantages to it over REST. But still, we use GraphQL as an alternative to REST, not a replacement.

Using GraphQL

Let’s get started with GraphQL

First, We need one dependency:

    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java</artifactId>
        <version>15.0</version>
    </dependency>

And a quick example:

public class HelloWorld {

    public static void main(String[] args) {
        String schema = "type Query{payload: String}";

        SchemaParser schemaParser = new SchemaParser();
        TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

        RuntimeWiring runtimeWiring = newRuntimeWiring()
                .type("Query", builder -> builder.dataFetcher("payload", new StaticDataFetcher(" hello world")))
                .build();

        SchemaGenerator schemaGenerator = new SchemaGenerator();
        GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);

        GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
        ExecutionResult executionResult = build.execute("{payload}");

        System.out.println(executionResult.getData().toString());
    }
}


OUTPUT:
{payload= hello world} 

We used following in our code:

  • Schema:-
    GraphQL API has a schema that defines each field that can be queried(i.e, “payload”).
    *You can’t query any field which is not here.
  • SchemaParser:-
    It is helpful if we need to parse our string into a schema.
  • TypeDefinitionRegistry:-
    It is the parsed version of our schema with the help of Schema Parser. We need this to create an executable schema.
  • RuntimeWiring:-
    It is used to fetch data from a specific field in the schema with the help of Data Fetchers(from “payload”).
  • StaticDataFetcher:-
    It fetches the Data for one field while the query is executed. It is static as we are putting hardcoded value(i.e, “hello world”).
  • SchemaGenerator:-
    It combines the TypeDefinitionRegistry with RuntimeWiring to actually make the GraphQLSchema.
  • GraphQLSchema:-
    We is use GraphQL Schema to create an executable with the help of schema generator.
  • Build:-
    It is to finally build our GraphQLSchema.
  • ExecutionResult:-
    It is to get the response from GraphQL after executing it’s build.

That’s pretty much it from the article. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing blogs like this for you in the future as well. Keep reading and Keep coding.

Reference

Knoldus-blog-footer-image

Leave a Reply