Table of contents
1.
Introduction😇
2.
GraphQL📈
3.
Create a GraphQL API Schema for Query Auto-Completion
4.
GraphQL vs. Rest
5.
Test GraphQL using Postman🧪🧑‍🔬
5.1.
Prerequisites📜
5.2.
Clone, install, and start the server🤖
5.3.
Review the Schema🤓
5.4.
Write a GraphQL query in Postman
6.
Frequently Asked Questions 🤔
6.1.
How do I set up an online GraphQL server?
6.2.
Can I use GraphQL with REST API?
6.3.
Which database is best for GraphQL?
7.
Conclusion🎉
Last Updated: Mar 27, 2024

Querying with GraphQL in Postman

Author Sagar Mishra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction😇

Postman is one of the most popular software testing tools used for API testing. Developers can quickly create, test, share, and document APIs with the aid of this tool. One doesn't need to create any HTTP client network code when using Postman for testing purposes. Instead, we create test libraries known as collections and let Postman interact with the API.

In this article, we will discuss the topic of Querying with GraphQL in Postman in detail.

GraphQL📈

Our first topic in the "Querying with GraphQL in Postman" series is GraphQL. Let's understand this in brief.

GraphQL is a query language and Server Side Runtime for APIs (Application Programming Interfaces) that prioritizes giving users exactly the data they request and nothing more. It is designed to make APIs fast, flexible, and developer-friendly.

GraphQL logo

GraphQL can even be deployed within an IDE that is Integrated Development Environment, known as GraphiQL. GraphQL is a REST option that enables devs to create requests. It combines data from various sources into a single API query.

Create a GraphQL API Schema for Query Auto-Completion

Our next topic in the "Querying with GraphQL in Postman" series is Creating API Schema. Here, we will learn to create a GraphQL API Schema for Query Auto-Completion.

A GraphQL schema describes the data users of GraphQL APIs can request. Also, it describes the mutation and queries that the client can use to read and publish data to the GraphQL server. In other words, your GraphQL schema must specify your client's or application's UI data requirements.

One of the most common features of Postman's GraphQL support is query auto-completion. The Postman API schemas are what power the auto-completion of queries. To develop a Postman GraphQL API schema:

⭐ In Postman, Go to the API Tab
 

⭐ Click on +New API
 

⭐ Give a name to your API
 

⭐ Ensure that you are in the Define Tab
 

⭐ From the dropdown menu, select GraphQL as your spec
 

⭐ Save after adding the Schema 
 

⭐ Go back to the Collection Tab
 

⭐ From the dropdown menu, attach your Schema
 

⭐ When you start typing, query auto-completion should function
 

⭐ Finally, you will see your GraphQL Response.

GraphQL vs. Rest

Now, we will discuss the difference between GraphQL and Rest API in the "Querying with GraphQL in Postman" series.

GraphQL vs. Rest logo
 

GraphQL vs. Rest

Test GraphQL using Postman🧪🧑‍🔬

The last topic is Test GraphQL using Postman in the "Querying with GraphQL in Postman" series.

There are mainly two options to test a GraphQL API. 

  • Apollo Explorer/ Apollo Studio
  • Postman

In this article, we will understand the second one, i.e., Postman. Any HTTP endpoint, including GraphQL, can be tested using this ideal tool.

Prerequisites📜

⚡ Installed the latest version of Git

 

⚡ NPM and node.js installed (version <8)

 

⚡  Postman Account or Postman agent installed

Clone, install, and start the server🤖

Paste the below code to pull the code from GitHub and start the server:

git clone https://github.com/apollographql/apollo-server-starter
cd apollo-server-starter 
npm install && npm run start:example discography

 

Congratulations! Your server is running if you see the below line:

🚀 Server ready at http://localhost:4000/

Review the Schema🤓

Before moving forward with this, first, let's check our Schema. The discography schema has definitions for albums, tracks, and artists. Also, two queries return either all albums or a particular album.

enum Genre {
  Pop,
  Rock,
  Alternative
  HipHop
}


type Track {
  title: String!
  number: Int!
}


type Artist {
  name: String!
}


type Album {
  title: String!
  artist: Artist!
  tracks: [Track!]!
  genre: Genre!
}


type Query {
  albums(genre: Genre): [Album!]!
  album(title: String!): Album
}

 

Write a GraphQL query in Postman

We will start by selecting the option to write the query in Postman.

The next step is to write the URL of the request. In this session, this (http://localhost:4000/) will be our URL.

GET Method

 

We can also change the methods to POST, PUT, DELETE, etc., by clicking on the dropdown arrow. We will change the method name from GET to POST as all GraphQL operations work on top of the HTTP POST method.

POST Method

 

Select GraphQL from the list of options below the Body selection by clicking it. A Query window and a GraphQL Variables window will be displayed to you.

GraphQL in POST method

 

Now we have to write our code in the Query Section. You can also copy the code given below.

query GetAllAlbums {
  albums {
    title 
    artist {
      name
    }
    tracks {
      title 
      number
    }
    genre
  }
}

 

Click on the Send button to generate the request. You will see the below page after clicking.

query

 

To apply sorting and filtering to our data so that we can view relevant data, we often need to build queries that return data and pass in variables. Use the below code in the Query Section. 

query GetAllAlbums ($genre: Genre) {
  albums (genre: $genre) {
    title 
    artist {
      name
    }
    tracks {
      title 
      number
    }
    genre
  }
}

 

The window should appear like this.

graphql variable

 

And after clicking on Send button, you will see the below screen.

response

Frequently Asked Questions 🤔

How do I set up an online GraphQL server?

Any environment that permits the deployment of "Node.js" projects is capable of hosting GraphQL servers created with apollo-server. To allow serverless deployment with AWS Lambda, apollo-server even offers versions.

There are guides for deployment available for:

👉 Heroku

👉 Lambda

👉 Azure Functions

Can I use GraphQL with REST API?

If you are a front-end developer team, who wants to sample GraphQL without requesting that the backend team create a GraphQL server, calling REST APIs from a GraphQL client unlocks the benefits of GraphQL for more individuals.

Which database is best for GraphQL?

There is a list of databases that can be used for GraphQL.

👉 LunaSec

👉 Back4App

👉 Weaviate

👉 ArangoDB

👉 FaunaDB

Conclusion🎉

We have discussed the topic of Querying with GraphQL in Postman. We have seen topics like GraphQL, creating a GraphQL API Schema, GraphQL vs. Res API, and Testing GraphQL using Postman.

We hope this blog has helped you enhance your knowledge of "Querying with GraphQL in Postman." If you want to learn more, check out our articles Purchasing and billingConfiguring SCIM in PostmanPostman to API Testing, and many more on our platform Coding Ninjas Studio. You can also check our blogs on:

  • Web Testing
  • API Testing
  • API

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

However, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass