graphql-http 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -190,11 +190,7 @@ fastify.all('/graphql', async (req, res) => {
190
190
  url: req.url,
191
191
  method: req.method,
192
192
  headers: req.headers,
193
- body: await new Promise((resolve) => {
194
- let body = '';
195
- req.on('data', (chunk) => (body += chunk));
196
- req.on('end', () => resolve(body));
197
- }),
193
+ body: req.body, // fastify reads the body for you
198
194
  raw: req,
199
195
  });
200
196
  res.writeHead(init.status, init.statusText, init.headers).end(body);
package/lib/handler.d.ts CHANGED
@@ -32,7 +32,13 @@ export interface Request<RawRequest, Context> {
32
32
  readonly method: string;
33
33
  readonly url: string;
34
34
  readonly headers: RequestHeaders;
35
- readonly body: string | Record<string, unknown> | null;
35
+ /**
36
+ * Parsed request body or a parser function.
37
+ *
38
+ * If the provided function throws, the error message "Unparsable JSON body" will
39
+ * be in the erroneous response.
40
+ */
41
+ readonly body: string | Record<string, unknown> | null | (() => string | Record<string, unknown> | null | Promise<string | Record<string, unknown> | null>);
36
42
  /**
37
43
  * The raw request itself from the implementing server.
38
44
  *
package/lib/handler.js CHANGED
@@ -142,16 +142,21 @@ function createHandler(options) {
142
142
  if (!req.body) {
143
143
  throw new Error('Missing body');
144
144
  }
145
+ let data;
145
146
  try {
146
- const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
147
- partParams.operationName = data.operationName;
148
- partParams.query = data.query;
149
- partParams.variables = data.variables;
150
- partParams.extensions = data.extensions;
147
+ const body = typeof req.body === 'function' ? await req.body() : req.body;
148
+ data = typeof body === 'string' ? JSON.parse(body) : body;
151
149
  }
152
- catch (_d) {
150
+ catch (err) {
153
151
  throw new Error('Unparsable JSON body');
154
152
  }
153
+ if (!(0, utils_1.isObject)(data)) {
154
+ throw new Error('JSON body must be an object');
155
+ }
156
+ partParams.operationName = data.operationName;
157
+ partParams.query = data.query;
158
+ partParams.variables = data.variables;
159
+ partParams.extensions = data.extensions;
155
160
  break;
156
161
  }
157
162
  default: // graphql-http doesnt support any other content type
@@ -229,7 +234,7 @@ function createHandler(options) {
229
234
  throw null;
230
235
  operation = ast.operation;
231
236
  }
232
- catch (_e) {
237
+ catch (_d) {
233
238
  return makeResponse(new graphql_1.GraphQLError('Unable to detect operation AST'), acceptedMediaType);
234
239
  }
235
240
  if (operation === 'subscription') {
package/lib/handler.mjs CHANGED
@@ -138,16 +138,21 @@ export function createHandler(options) {
138
138
  if (!req.body) {
139
139
  throw new Error('Missing body');
140
140
  }
141
+ let data;
141
142
  try {
142
- const data = typeof req.body === 'string' ? JSON.parse(req.body) : req.body;
143
- partParams.operationName = data.operationName;
144
- partParams.query = data.query;
145
- partParams.variables = data.variables;
146
- partParams.extensions = data.extensions;
143
+ const body = typeof req.body === 'function' ? await req.body() : req.body;
144
+ data = typeof body === 'string' ? JSON.parse(body) : body;
147
145
  }
148
- catch (_d) {
146
+ catch (err) {
149
147
  throw new Error('Unparsable JSON body');
150
148
  }
149
+ if (!isObject(data)) {
150
+ throw new Error('JSON body must be an object');
151
+ }
152
+ partParams.operationName = data.operationName;
153
+ partParams.query = data.query;
154
+ partParams.variables = data.variables;
155
+ partParams.extensions = data.extensions;
151
156
  break;
152
157
  }
153
158
  default: // graphql-http doesnt support any other content type
@@ -225,7 +230,7 @@ export function createHandler(options) {
225
230
  throw null;
226
231
  operation = ast.operation;
227
232
  }
228
- catch (_e) {
233
+ catch (_d) {
229
234
  return makeResponse(new GraphQLError('Unable to detect operation AST'), acceptedMediaType);
230
235
  }
231
236
  if (operation === 'subscription') {
package/lib/utils.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import type { ExecutionResult, GraphQLError } from 'graphql';
7
7
  /** @private */
8
- export declare function isObject(val: unknown): val is Record<PropertyKey, unknown>;
8
+ export declare function isObject(val: unknown): val is Record<PropertyKey, any>;
9
9
  /** @private */
10
10
  export declare function areGraphQLErrors(obj: unknown): obj is readonly GraphQLError[];
11
11
  /** @private */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-http",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Simple, pluggable, zero-dependency, GraphQL over HTTP Protocol compliant server and client",
5
5
  "keywords": [
6
6
  "graphql",
Binary file