Skip to content

Commit d3fff0d

Browse files
committed
gendocs
1 parent faf3b2e commit d3fff0d

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

docs/modules/use_express.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ console.log('Listening to port 4000');
6868

6969
`Handler`
7070

71-
## Server/http
71+
___
7272

7373
### parseRequestParams
7474

@@ -88,25 +88,21 @@ import { parseRequestParams } from 'graphql-http/lib/use/express';
8888

8989
const app = express();
9090
app.all('/graphql', async (req, res) => {
91-
if (req.url.startsWith('/graphql')) {
92-
try {
93-
const maybeParams = await parseRequestParams(req, res);
94-
if (!maybeParams) {
95-
// not a well-formatted GraphQL over HTTP request,
96-
// parser responded and there's nothing else to do
97-
return;
98-
}
99-
100-
// well-formatted GraphQL over HTTP request,
101-
// with valid parameters
102-
res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
103-
} catch (err) {
104-
// well-formatted GraphQL over HTTP request,
105-
// but with invalid parameters
106-
res.writeHead(400).end(err.message);
91+
try {
92+
const maybeParams = await parseRequestParams(req, res);
93+
if (!maybeParams) {
94+
// not a well-formatted GraphQL over HTTP request,
95+
// parser responded and there's nothing else to do
96+
return;
10797
}
108-
} else {
109-
res.writeHead(404).end();
98+
99+
// well-formatted GraphQL over HTTP request,
100+
// with valid parameters
101+
res.writeHead(200).end(JSON.stringify(maybeParams, null, ' '));
102+
} catch (err) {
103+
// well-formatted GraphQL over HTTP request,
104+
// but with invalid parameters
105+
res.writeHead(400).end(err.message);
110106
}
111107
});
112108

docs/modules/use_fastify.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### Functions
1616

1717
- [createHandler](use_fastify.md#createhandler)
18+
- [parseRequestParams](use_fastify.md#parserequestparams)
1819

1920
## Server/fastify
2021

@@ -66,3 +67,56 @@ console.log('Listening to port 4000');
6667
#### Returns
6768

6869
`RouteHandler`
70+
71+
___
72+
73+
### parseRequestParams
74+
75+
**parseRequestParams**(`req`, `reply`): `Promise`<[`RequestParams`](../interfaces/common.RequestParams.md) \| ``null``\>
76+
77+
The GraphQL over HTTP spec compliant request parser for an incoming GraphQL request.
78+
79+
If the HTTP request _is not_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), the function will respond
80+
on the `Response` argument and return `null`.
81+
82+
If the HTTP request _is_ a [well-formatted GraphQL over HTTP request](https://graphql.github.io/graphql-over-http/draft/#sec-Request), but is invalid or malformed,
83+
the function will throw an error and it is up to the user to handle and respond as they see fit.
84+
85+
```js
86+
import Fastify from 'fastify'; // yarn add fastify
87+
import { parseRequestParams } from 'graphql-http/lib/use/fastify';
88+
89+
const fastify = Fastify();
90+
fastify.all('/graphql', async (req, reply) => {
91+
try {
92+
const maybeParams = await parseRequestParams(req, reply);
93+
if (!maybeParams) {
94+
// not a well-formatted GraphQL over HTTP request,
95+
// parser responded and there's nothing else to do
96+
return;
97+
}
98+
99+
// well-formatted GraphQL over HTTP request,
100+
// with valid parameters
101+
reply.status(200).send(JSON.stringify(maybeParams, null, ' '));
102+
} catch (err) {
103+
// well-formatted GraphQL over HTTP request,
104+
// but with invalid parameters
105+
reply.status(400).send(err.message);
106+
}
107+
});
108+
109+
fastify.listen({ port: 4000 });
110+
console.log('Listening to port 4000');
111+
```
112+
113+
#### Parameters
114+
115+
| Name | Type |
116+
| :------ | :------ |
117+
| `req` | `FastifyRequest`<`RouteGenericInterface`, `RawServerDefault`, `IncomingMessage`, `FastifySchema`, `FastifyTypeProviderDefault`, `unknown`, `FastifyBaseLogger`, `ResolveFastifyRequestType`<`FastifyTypeProviderDefault`, `FastifySchema`, `RouteGenericInterface`\>\> |
118+
| `reply` | `FastifyReply`<`RawServerDefault`, `IncomingMessage`, `ServerResponse`<`IncomingMessage`\>, `RouteGenericInterface`, `unknown`, `FastifySchema`, `FastifyTypeProviderDefault`, `unknown`\> |
119+
120+
#### Returns
121+
122+
`Promise`<[`RequestParams`](../interfaces/common.RequestParams.md) \| ``null``\>

0 commit comments

Comments
 (0)