Build Query API using GraphQL

           

Build Query API using GraphQL

Introduction

     This article explains about building the query API using GraphQL. This article also helps to test these API after deploying to cloud containers.

Pre-Requisite

  •   Install graphiql\postman client for testing.

Link: https://www.electronjs.org/apps/graphiql (for installing graphiql)

What is GraphQL?

  • GraphQL is a query language designed to build client applications
  • It is used to query application servers that have capabilities defined in the specification
  • Refer below link for more information: http://spec.graphql.org/

Why GraphQL:

  • GraphQL is an open-source server-side technology to optimize RESTful API calls.
  • It does not mandate a programming language for application servers that implement it.
  • It structures data in the form of a graph with its powerful query syntax for traversing, retrieving, and modifying data
  • Applications using it are fast and stable
  • It gives the liberty to restrict data that should be fetched from the server

      e.g.:

Consider a business object Account with the attributes Accountid, Name and AccountType. Suppose a mobile application needs to fetch only the Accountid and AccountType. If we design a REST endpoint like /api/v1/Accounts, it will end up fetching data for all the fields for Account object. This means, data is over fetched by the RESTful service. This problem can be solved by GraphQL.

GraphQL solved above case using this query:

 

Introduction to GraphQL terminology:

Query:

  • GraphQL query is used to read or fetch values from server

     e.g.:

 

Mutation:  

  • GraphQL mutation is used to write or post values to server.

        e.g.:

      

Schema:

  • A GraphQL schema describes the functionality available to the client applications
  • The GraphQL runtime defines a generic graph-based schema to publish the capabilities of the data service it represents.
  • Using this Client applications can query the schema within its capabilities.

e.g.:

        AccountSchema.txt:

type Query {

   accounts:[Accounts]

   accountById(id:ID!):Accounts

}

type Mutation {

CreateAccounts(firstName:String!,lastName:String!): Accounts

}

type Accounts {

   id:ID!

   firstName:String

   lastName:String

   fullName:String

}

Create GraphQL descriptor on Integration Server:

  • Right click on GraphqlAccounts and click on create GraphQL Descriptor

  • Name the element as account_graphqlService
  • In this case import the Schema AccountSchema.txt

            

  • Click on finish.
  • Count of resolver service depends upon the schema imported.

             

  • GraphQL descriptor creates below services as resolvers.

             a.  CreateAccountsResolver as mutation service

             b. acountsResolver and accountByIdResolver as query service.

             c. Accounts as type (documentType).

                

  • account_graphqlService contains the definition of query and mutation.

 

  • In this case, resolvers’ services are implemented.

                          

Testing Using GraphiQL client:

  • Setup the Graphiql endpoints.

Endpoints:

http://<hostname>:<port>/graphql/<graphql_fully_qualified name>

graphql descriptor name: Right click on account_graphqlservice and click copy

                         

                   

Endpoint URL In this case : http://localhost:6565/graphql/graphqlaccounts:account_graphqlservice

  • Configure the authorization in headers in graphiql client

            

Test accounts Resolver:

  • Pass the below input :
            {

accounts{

    id

    firstName

    lastName

  }

}
  • In this case expected Response
"data": {

"accounts": [

{

"id": "1",

"firstName": "John",

"lastName": "Dave"

}

{

"id": "2",

"firstName": "John",

"lastName": "Miachel"

}

]

}

Test CreateAccounts

  • Query can be modified as per requirement on graphiql client
  • Pass the below input:
mutation {

   CreateAccounts(firstName:"Susan",lastName:"Billy") {     

      firstName

    lastName

    fullName

   }

}
  • In this case, expected response:
{

  "data": {

    "CreateAccounts": {

      "firstName": "Suasane",

      "lastName": "Billy",

      "fullName": "SuasaneBilly"

    }

  }

}

Deployment to Cloud Container:

Connect to Cloud Container

  • Navigate to GraphqlAccounts package
  • Package contains resolvers and type
  • Resolvers contains the services for mutation and query
  • Type contains the Account documentType.

        

    

  • Navigate to resolvers and open accountsResolver

       

  •  Click on Test

        

      

Test Using GraphiQL client

  •  All the services deployed to cloud container provides endpoints

        

    

  • Configure endpoints and basic authorization
  • Trigger the request.

Input Request:

                         mutation {

   CreateAccounts(firstName:"Susan",lastName:"Billy") {     

      firstName

    lastName

    fullName

   }

}

 

 

Test Using Postman client

  • Import the postman collection

          https://www.getpostman.com/collections/346321b27723f52589bc

  • Test the service using postman client
     Note: Attached the Postman collection,IS package (GraphqlAccounts) and Account schema(Accounts.txt) for your reference.