Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> 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 {
    writePost(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:

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:

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 {
    writePost (
        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 name of the operation 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:

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 for it.

A collection of requests can be found over on GitHub.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!