Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Junit footer banner
Comments are closed on this article!