GraphQL

GraphQL Query with Example

January 29, 2018

author:

GraphQL Query with Example

In REST APIs, data is usually loaded from specific endpoints. All of these endpoints returns information as per pre-defined structure. Thus the URL holds encoded data as per client’s requirements.

But GraphQL works on a different approach altogether. APIs built with GraphQL typically engage only a single endpoint as opposed to multiple endpoints of REST. This typically works well as the structure of the data is decided by the client which may vary with time. This increases the efficiency of the application and flexibility of the API. But due to this, a client has to send more information to the server while requesting data, and that’s where Queries comes handy.

# What is GraphQL Query

To keep things aligned with REST, we can remember GraphQL queries as a GET route for our API. Fortunate enough, we don’t really need to run the syntax of GraphQL queries as its very similar to the JSON objects, but without data.

# GraphQL Query Example

For instance, if this is the JSON Object:

const Posts = [
    { 
        title: "Some Title", 
        body: "Some Body", 
        author: "Khushi Shah" 
    },
    { 
        title: "Title goes here", 
        body: "Body goes here", 
        author: "Khushi Shah" 
    }
];

Then, a corresponding GraphQL query can be:

query getAllPosts {
  getPosts {
    title
  }
}

And, the resultant response would be:

{
 "data": {
  "allPosts ": [
      {
        "title": "Some Title"
      },
      {
        "title": "Title goes here"
      }
    ]
  }
}

I’m sure you noted that the response returns only title of the post and not other information like body or author. That is because the client has explicitly asked for only title field. He would, of course, return body as well with:

query getAllPosts {
  getPosts {
    title
    body
  }
}

# Nested GraphQL Query

Additionally, GraphQL also allows nested queries for relationships:

query getAllPosts {
  getPosts {
    title
    body
    author {
     name
    }
  }
}

# GraphQL Query Arguments

We can also attach parameters to the query given that it is also mentioned in the SDL Schema:

{
  getAuthors(last: 2) {
    name
  }
}

In the above mentioned instance, the Author field type might have the last parameter that returns only a specific number of Authors.

# Define Query in GraphQL Schema

To make the above queries work, we also need to setup its schema. Schema is usually configured on the back-end platform. We can configure Query entry point for getAuthors as:

type Query {
 getAuthors: [Author!]! 
}

To let the client pass arguments with their queries, we can setup our schema as:

type Query {
  getAuthors(last: Int): [Author!]!
}

Parting Thoughts:

Query is an important part of GraphQL ecosystem. We learned how to fetch data from API using queries. We also had a look at passing arguments with query and in the end, attached it in the GraphQL Schema.

Leave a comment

Your email address will not be published. Required fields are marked *