Skip to content

Commit ac21733

Browse files
committed
add guide on graphql-http errors
1 parent fd2e903 commit ac21733

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

src/pages/learn/_meta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ export default {
2727
performance: "",
2828
security: "",
2929
federation: "",
30+
"debug-errors": "Common `graphql-http` Errors",
3031
}

src/pages/learn/best-practices.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@ The articles in this section should not be taken as gospel, and in some cases ma
5353
link: "/learn/security/",
5454
description: "Protect GraphQL APIs from malicious operations",
5555
},
56+
{
57+
title: "Common Errors",
58+
link: "/learn/debug-errors/",
59+
description: "Learn about common `graphql-http` errors and how to debug them.",
60+
}
5661
]}
5762
/>

src/pages/learn/debug-errors.mdx

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Common `graphql-http` Errors and How to Debug Them
2+
3+
When building or consuming a GraphQL API over HTTP, it's common to run into
4+
errors, especially during development. Understanding how to recognize and resolve these issues
5+
can save you time and frustration.
6+
7+
This guide outlines common `graphql-http` errors, what they mean, and how to debug them
8+
effectively. These examples assume you're using the
9+
[GraphQL over HTTP specification](https://graphql.github.io/graphql-over-http/draft/)
10+
and an implementation such as [`graphql-http`](https://github.com/graphql/graphql-http) or any
11+
server that follows the spec.
12+
13+
## `400 Bad Request`: Syntax or parse errors
14+
15+
### What it means
16+
17+
The server couldn't parse your request. Either the GraphQL query string is malformed,
18+
or the JSON body isn't valid.
19+
20+
### Common causes
21+
22+
- JSON syntax errors
23+
- Sending a plain string without wrapping it in `{ "query": "..." }`
24+
- Using `Content-Type: application/graphql` without supporting it
25+
26+
### How to debug
27+
28+
- Validate your JSON body using a linter or formatter.
29+
- Make sure you're sending a `POST` request with a `Content-Type: application/json` header.
30+
- Verify that the GraphQL query is syntactically correct. Use an IDE or a linter to verify it.
31+
32+
## `405 Method Not Allowed`: Wrong HTTP Method
33+
34+
### What it means
35+
36+
You're using an unsupported HTTP method. Most GraphQL servers require `POST` for mutations
37+
and may allow `GET` for queries.
38+
39+
### Common causes
40+
41+
- Sending a `PUT` or `DELETE` request instead of `POST` or `GET`
42+
- Sending a `GET` request for a mutation
43+
44+
### How to debug
45+
46+
- Check your HTTP method. Mutations must use `POST`.
47+
- Make sure your server supports `GET` for queries.
48+
- Refer to the [GraphQL over HTTP spec](https://graphql.github.io/graphql-over-http/draft/) to
49+
confirm method support.
50+
51+
## `415 Unsupported Media Type`
52+
53+
### What it means
54+
55+
The server doesn't understand the request's `Content-Type`.
56+
57+
### Common causes
58+
59+
- Sending a GraphQL query with `Content-Type: text/plain` or another unsupported type
60+
- Omitting the `Content-Type` header in a `POST` request
61+
62+
### How to debug
63+
64+
- Set the header explicitly: `Content-Type: application/json`.
65+
- If you're using `application/graphql`, verify your server supports it. This content type
66+
is optional.
67+
68+
## `422 Unprocessable Entity`: Missing or invalid `query`
69+
70+
### What it means
71+
72+
The server received the request, but the `query` field was missing or invalid.
73+
74+
### Common causes
75+
76+
- Sending `{}` with no `query` key
77+
- Sending variables or an operation name without a query
78+
79+
### How to debug
80+
81+
Always include a `query` field in your request body. For example:
82+
83+
```json
84+
{
85+
"query": "{ user(id: 1) { name } }"
86+
}
87+
```
88+
89+
## `500 Internal Server Error`: Unexpected server failures
90+
91+
### What it means
92+
93+
Something went wrong on the server.
94+
95+
### Common causes
96+
97+
- An unhandled exception in a resolver
98+
- Schema validation issues during server startup
99+
- Missing or misconfigured middleware
100+
101+
### How to debug
102+
103+
- Check server logs or stack traces.
104+
- Add error handling to resolvers.
105+
106+
## GraphQL errors with `200 OK`
107+
108+
### What it means
109+
110+
The HTTP layer succeeded, but the GraphQL response contains errors.
111+
112+
### Common causes
113+
114+
- Querying a field that doesn't exist
115+
- Passing incorrect arguments to a field
116+
- Violating schema constraints, such as non-nullability
117+
118+
### How to debug
119+
120+
Check the `errors` array in the response body. A typical response looks like:
121+
122+
```json
123+
{
124+
"data": null,
125+
"errors": [
126+
{
127+
"message": "Cannot query field \"foo\" on type \"Query\".",
128+
"locations": [{ "line": 1, "column": 3 }]
129+
}
130+
]
131+
}
132+
```
133+
134+
Compare your query against the schema using introspection or an IDE.

0 commit comments

Comments
 (0)