|
15 | 15 | ### Functions
|
16 | 16 |
|
17 | 17 | - [createHandler](use_fastify.md#createhandler)
|
| 18 | +- [parseRequestParams](use_fastify.md#parserequestparams) |
18 | 19 |
|
19 | 20 | ## Server/fastify
|
20 | 21 |
|
@@ -66,3 +67,56 @@ console.log('Listening to port 4000');
|
66 | 67 | #### Returns
|
67 | 68 |
|
68 | 69 | `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 `FastifyReply` 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