Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this short tutorial, we’ll show how to test GraphQL endpoints using Postman.

2. Schema Overview and Methods

We’ll use the endpoints created in our GraphQL tutorial. As a reminder, the schema contains definitions describing posts and authors:

type Post {
    id: ID!
    title: String!
    text: String!
    category: String
    author: Author!
}
 
type Author {
    id: ID!
    name: String!
    thumbnail: String
    posts: [Post]!
}

Plus, we’ve got methods for displaying posts and writing new ones:

type Query {
    recentPosts(count: Int, offset: Int): [Post]!
}
 
type Mutation {
    createPost(title: String!, text: String!, category: String) : Post!
}

When using a mutation to save data, the required fields are marked with an exclamation mark. Also note that in our Mutation, the returned type is Post, but in Query, we’ll get a list of Post objects.

The above schema can be loaded in the Postman API section — just add New API with GraphQL type and press Generate Collection:

graphql schema generator 1

Once we load our schema, we can easily write sample queries using Postman’s autocomplete support for GraphQL.

3. GraphQL Requests in Postman

First of all, Postman allows us to send the body in GraphQL format — we just choose the GraphQL option below:

GraphQL 1

Then, we can write a native GraphQL query, like one that gets us the title, category, and author name into the QUERY section:

query {
    recentPosts(count: 1, offset: 0) {
        title
        category
        author {
            name
        }
    }
}

And, as a result, we’ll get:

{
    "data": {
        "recentPosts": [
            {
                "title": "Post",
                "category": "test",
                "author": {
                    "name": "Author 0"
                }
            }
        ]
    }
}

It’s also possible to send a request using the raw format, but we have to add Content-Type: application/graphql to the headers section. And, in this case, the body looks the same.

For example, we can update title, text, category, get an id and title as a response:

mutation {
    createPost (
        title: "Post", 
        text: "test", 
        category: "test",
    ) {
        id
        title
    }
}

The type of operation – like query and mutation – can be omitted from the query body as long as we use a shorthand syntax. In this case, we can’t use the operation’s name and variables, but it’s recommended to use the operation name for easier logging and debugging.

4. Using Variables

In the variables section, we can create a schema in JSON format that will assign values to the variables. This avoids typing arguments in a query string:

graphql variables

So, we can modify the recentPosts body in the QUERY section to dynamically assign values from variables:

query recentPosts ($count: Int, $offset: Int) {
    recentPosts (count: $count, offset: $offset) {
        id
        title
        text
        category
    }
}

And we can edit the GRAPHQL VARIABLES section with what we’d like our variables to be set to:

{
  "count": 1,
  "offset": 0
}

5. Summary

We can easily test GraphQL using Postman, which also allows us to import the schema and generate queries.

A collection of requests can be found on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!