Submit GraphQL operations over HTTP
Calling queries or mutations from a web or backend client.
GraphQL · Const
GraphQL is commonly served through one HTTP endpoint, typically /graphql. Clients send JSON requests, usually with POST, and receive results under data plus any validation or execution errors under errors.
In GraphQL HTTP handling, authentication runs before request validation so resolvers can access session and user information. The request is usually sent as JSON to one service endpoint, with the operation in the body; query operations may also use GET.
POST /graphql
Content-Type: application/json
Accept: application/graphql-response+jsonA JSON-encoded response containing the operation result under data and any validation or execution errors under errors.
/graphql.const query = `query { viewer { id name } }`;
fetch("https://api.example.com/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/graphql-response+json",
},
body: JSON.stringify({ query }),
});A client sends a GraphQL query to the service endpoint as a JSON HTTP POST request.
Calling queries or mutations from a web or backend client.
Supporting clients that need cacheable, read-only requests.
Returning predictable machine-readable results and failures.
GraphQL commonly uses one endpoint, typically /graphql, rather than a URL per entity. Requests identify the operation and selection set in the GraphQL document sent to that endpoint.
Follow-up: Can GraphQL use a transport other than HTTP? Yes. GraphQL is transport-agnostic, although HTTP is the common choice.
A server must accept POST for query and mutation operations. It may also accept GET for query operations, but GET is not the general-purpose method for mutations.
Follow-up: What should a client send to indicate the desired response format? It should include application/graphql-response+json in the Accept header.
The JSON response places the full or partial successful result under data. Validation or execution problems are reported under errors; both keys can occur when execution returns partial data.
Follow-up: Where should authorization be implemented? In the business logic during GraphQL execution, after authentication has established the user context.
HTTPauthenticationauthorizationJSONPOSTGETAccept headerContent-Type header