How to send graphql query by postman?
Asked Answered
N

14

92

I use

POST type
URL http://######/graphql
Body: 
query: "query: "{'noteTypes':  {'name', 'label', 'labelColor', 'groupName', 'groupLabel', 'imageUrl'}}"

But it return "message": "Must provide query string."

Noseband answered 28/2, 2017 at 23:32 Comment(0)
B
147

There's a better way to do it using the REST client Insomnia

Docs are here, how to send graphql queries: https://support.insomnia.rest/article/61-graphql


Below are the steps for postman

Step 1.

Run the GraphiQL in Chrome, open the Chrome Dev Console, click the Network tab, and make the query from graphiql, when you make the query, network tab will show the graphql request...

enter image description here

Step 2.

From the graphql request copy the request query, Select the Copy as cURL (cmd)

enter image description here

Step 3.

Open Postman, In the Top-Left click on the Import button, after you click Import you have to click the Paste Raw Text, and paste the copied cURL request as done in step2 after it's done click the Import

enter image description here

Step 4.

Postman is ready to send the Graphql request, Just Click on the Send Button, you will see the Response in the Response Box in body as below

enter image description here

Step 5.

To see how the query is being sent click on the Body tab next to Headers, you will get know how to provide the fields from postman in JSON format.

e.g: edges {\n node {\n id\n jobId\n }\n, If you want to view another field then you need to add it in with the suffix \n

like if need name then : edges {\n node {\n id\n jobId\n name\n }\n

\n here just means to represent a new line. Instead, you can make it simpler by providing a clear and illustrative JSON like below

===========================================================================

Note: The body type must be raw with application/json content-type. So, the query must be a valid JSON with quotes ".."

{  
   "query":"{viewer {user {edges {node {id jobId name }}}}}"
}

===========================================================================

enter image description here

you can directly start from step 5 if you know how to send the query in body and other things too that needs to be required while making a request from postman

enter image description here

With simplified JSON

enter image description here

Bendite answered 1/3, 2017 at 5:14 Comment(4)
While using the same steps I am getting:, Error while importing Curl: 2option-less arguments found. Only one is supported (the URL)Mohr
Copy as cURL(bash) worked for me, instead of Copy as cURL(cmd), using chrome postman app on windows 10 OSSeptavalent
I just change the Content-Type with application/graphql to application/json, and it work, I think the postman default setting this as application/graphql is so bad.Councillor
Keep in mind that GraphQL requests are POST requests. Not GET.Pressroom
H
52

You don't need INSOMNIA in case the GraphQL server responds to Content-type: application/graphql or postman.setEnvironmentVariable,

Just do it:

In Headers tab: Content-Type: application/graphql

In Body tab, "raw" selected, put your query

enter image description here

Holiness answered 23/4, 2018 at 19:23 Comment(2)
i get "No query string was present" trying thisBribe
Works perfectly for me! Using Node.js with express and express-graphql dependencies.Strickland
I
26

Adding this for anyone searching on the topic ... you can utilize and test GraphQL calls far better and more easily with Insomnia:

https://insomnia.rest

It's been fantastic for GraphQL development.

Iraqi answered 26/9, 2017 at 15:21 Comment(1)
Thank you! Here is the doc regarding GraphQL: support.insomnia.rest/article/61-graphqlCowen
J
20

There's a simple way to do it. Use a pre-request script to stringify the payload (source).

Step 1.

In the body of the request put a placeholder for the payload.

{
    "query":{{query}}
}

Step 2.

Create the payload in the pre-request script and store it in an environment variable.

postman.setEnvironmentVariable("query", JSON.stringify(
`
{
  search(query: "test", type: ISSUE, first: 10) {
    issueCount
    edges {
      node {
        ... on Issue {
          title
          id
          state
          closed
          repository {
            name
          }
        }
      }
    }
  }
}
`
));

That's it.

Juryman answered 1/11, 2017 at 14:23 Comment(2)
This is working like a charm and avoids to go through the hassle of all the manual steps described in the previous answers! Thank you.Wanton
We must be using an older version of graphql or having a specific setup but it finally worked with the above proposal and a tiny change "body" instead of "query": > placeholder > { > "body":{{query}} > }Gerigerianna
L
15

UPDATE 8-2019 - I know this is old, but regarding POSTMAN, if you haven't figured it out already, they do have a graphql (beta) option for posting body. There is no need to add any additional headers.

enter image description here

Limicoline answered 18/8, 2019 at 15:28 Comment(1)
Nowadays Postman's GraphQL body type aren't in beta anymore! It is a stable option available, as shown hereBilious
R
11

UPDATE 2:

It's not practical use POSTMAN, because the are working yet in a easy way to add headers, that take longtime, and i think POSTMAN is not made for work naturally with graphql,

you can follow the progress about that here: https://github.com/postmanlabs/postman-app-support/issues/1669

I recommend to use another packages plugin like:

the best (like postman , but profile and sync price 5$ monthly):
   https://insomnia.rest/
others:
   https://github.com/andev-software/graphql-ide
   https://github.com/imolorhe

for graphiql (no add headers possibility) you need to set three things (it's not easy to type):

  1. Header:

    Content-Type: application/json

  2. Body:

    Choose Ray < optiongroup

    Choose JSON (application/json) < selectbox

  3. Compose javascript object with "query" and the "value" of your graph query. Like all objects in js it'sneeded the propery and the value , in this case "quote" is the property, the value must be with double quotes. Inside the value (graphl string) you dont compose js objects, so you dont need use doble quotes, it's just a string.

    {"query":"{ allQuotes { text } }" }

    the problem is you need type all in a single line, no like grapIql... there is a post requirement in postman github so is easy work with graphql:

enter image description here

enter image description here

Redbreast answered 8/8, 2017 at 8:25 Comment(0)
P
11

Postman just released inbuilt GraphQL support in version 7.2.

This version supports

  • Sending GraphQL queries in request body as POST requests
  • Support for GraphQL variables
  • Creating APIs in Postman with GraphQL schema type
  • Query autocompletion integrated with user defined GraphQL schemas

Please give it a try and give us your feedback on the tracking thread on our community forum

Pinetum answered 3/6, 2019 at 19:29 Comment(0)
G
10

I faced the same problem when I try to used graphQl query using POSTMAN, In POSTMAN send data from the raw tab with json type.

Query Command:

{"query":"{user(id:902){id,username,DOB}}"}

Mutations Command:

{ "query": "mutation {createMutations(reviewer:36, comments:\"hello\",data_id: 1659, approved: true ){id}}" }

       #commnent: String Type
       #data_id:Int Type
       #approved:Boolean Type
Garv answered 26/4, 2018 at 12:46 Comment(1)
Could you please tell us how to pass an object? Here if reviewer and comments are part of the new input object the how the command will be?Ianteen
C
6

If you're using Visual Studio, I have written a plugin to convert GraphQL to Restful body

https://marketplace.visualstudio.com/items?itemName=orasik.graphql-to-rest-queries

enter image description here

Cattle answered 25/3, 2019 at 13:8 Comment(1)
You saved my weekend :)Brainless
D
5

Postman has recently launched its out of box support for GraphQL: https://blog.getpostman.com/2019/06/18/postman-v7-2-supports-graphql/

Below is the screenshot of testing GraphQL locally:

enter image description here

Note: Running GraphQL locally using spring-boot https://www.baeldung.com/spring-graphql

Deadman answered 27/6, 2019 at 14:49 Comment(0)
L
3

Deriving from Estevão Lucas' answer.

You can also use header Content-type: application/json on postman

And define the body with:

{
    "query": "{ your_query }"
}

This is easily constructed on the client side to form a request payload.

e.g.

enter image description here

Output:

enter image description here

Linguiform answered 13/3, 2019 at 16:1 Comment(3)
Could you please tell me how to pass an object. For example, email and password as part of another object then how request payload change?Ianteen
I'm not sure what you mean, but what I'm understanding is you want to pass an object e.g. credentials which has the property of password and email. In that case, you may need to define an input type object in your mutation. and use the $input: <yourinputmutation>! or something like that in the query.Basifixed
Fore more code example here: github.com/roelzkie15/chatta-api/blob/master/services/messaging/…Basifixed
F
1

Checkout https://github.com/hasura/graphqurl - curl for GraphQL:

  • CLI for making GraphQL queries with autocomplete
  • Run GraphiQL locally against any endpoint (with custom headers)
  • Use as a library with nodejs or from the browser
  • Supports subscriptions

I am one of the authors.

gq https://gentle-anchorage-72051.herokuapp.com/v1alpha1/graphql -i

GraphiQL with headers

Fain answered 10/8, 2018 at 13:27 Comment(0)
J
0

IF we can pass header type, Then add the header Content-type: application/graphql

Below link can be used as reference: link description here

Jadwigajae answered 25/4, 2019 at 10:41 Comment(0)
W
0

By adding header we can run graphql query in the postman

Content-type: application/graphql

Woozy answered 9/9, 2019 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.