Daryn
Daryn

Reputation: 3768

Can you explain the terminology in a GraphQL POST request

In the Apollo Developers documentation there is an example for a valid body of a post request in GraphQL

{
    "query": "query aTest($arg1: String!) { test(who: $arg1) }"
}

From what I have seen in Apollo Server this query would also work with:

{
    "query": "aTest($arg1: String!) { test(who: $arg1) }"
}

Could someone please explain why "query" appears twice in the first example? What does the GraphQL server interpret from this?

Is the second example a standardised format?

Ref: http://dev.apollodata.com/tools/graphql-server/requests.html

Upvotes: 0

Views: 529

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84687

Query can have two meanings in GraphQL -- it can refer to the request being made to the server, or to to the type of operation used within that request. There are three types of operations used in GraphQL -- Query, Mutation and Subscription.

In your first example, the first query identifies the query document being sent with your request. The second query identifies the type of operation.

In other words, you can query a server, but your query may or may not contain a Query (it may, for example, be a Mutation).

When writing your query, if you omit the type of operation, GraphQL simply assumes you wanted a Query, not some other operation and rolls with it. In a similar fashion, it's possible to omit the operation name (aTest) as well.

AFAIK, there's no benefit to omitting the operation name -- you should always include it to prevent any ambiguity and potential issues down the line.

Upvotes: 1

Related Questions