Skip to content

Commit

Permalink
feat: reduced what's accessible from response functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: GraphQL response functions only have access to variables, context and updateContext
(query and operationName removed). HTTP response functions only have access to body, query, params,
context and updateContext (all other express request fields removed).
  • Loading branch information
kenneth-gray committed Feb 16, 2020
1 parent 63f1d28 commit 546e53b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,12 @@ See [HttpMock](#httpmock) and [GraphQlMock](#graphqlmock) for more details.
### GraphQlResponseFunction

> `function({ operationName, query, variables, context, updateContext }): response | Promise<response>`
> `function({ variables, context, updateContext }): response | Promise<response>`
<!-- https://www.tablesgenerator.com/markdown_tables -->

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| operationName | `string` | `''` | operationName sent by client. |
| query | `string` | `''` | GraphQL query. |
| variables | `object` | `{}` | variables sent by client. |
| context | `object` | `{}` | Data stored across API calls. |
| updateContext | `Function` | `partialContext => updatedContext` | Used to update context. |
Expand Down
6 changes: 1 addition & 5 deletions src/graph-ql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ type GraphQlHandler = (
operationType: 'query' | 'mutation';
operationName: string;
variables: Record<string, any>;
query: string;
},
res: Response,
) => boolean;
Expand Down Expand Up @@ -87,15 +86,13 @@ function createGraphQlHandler({
}): GraphQlHandler {
const handler = createHandler(rest);

return ({ operationType, operationName, query, variables }, res) => {
return ({ operationType, operationName, variables }, res) => {
if (
operationType === operationTypeToCheck &&
operationName === operationNameToCheck
) {
handler(
{
operationName,
query,
variables,
},
res,
Expand Down Expand Up @@ -180,7 +177,6 @@ function createGraphQlRequestHandler(handlers: GraphQlHandler[]) {
operationType,
operationName,
variables,
query,
},
res,
);
Expand Down
12 changes: 7 additions & 5 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Router } from 'express';
import { Router, RequestHandler } from 'express';

import { createHandler } from './create-handler';
import { Mock, HttpMock, Context, UpdateContext } from './types';
Expand Down Expand Up @@ -40,19 +40,21 @@ function applyHttpRoutes({
updateContext,
getContext,
});
const requestHandler: RequestHandler = ({ body, query, params }, res) =>
handler({ body, query, params }, res);

switch (method) {
case 'GET':
router.get(url, handler);
router.get(url, requestHandler);
break;
case 'POST':
router.post(url, handler);
router.post(url, requestHandler);
break;
case 'PUT':
router.put(url, handler);
router.put(url, requestHandler);
break;
case 'DELETE':
router.delete(url, handler);
router.delete(url, requestHandler);
break;
default:
throw new Error(
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ export type Operation = {
} & ResponseProps<
MockResponse<
{
operationName: string;
query: string;
variables: Record<string, any>;
},
GraphQlResponse | HttpResponse
Expand Down

0 comments on commit 546e53b

Please sign in to comment.