Skip to content

Commit 4d48034

Browse files
committed
Add docs and tests for graphQLParams
1 parent caeb3ac commit 4d48034

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,26 @@ The `graphqlHTTP` function accepts the following options:
144144

145145
- **`typeResolver`**
146146

147+
In addition to an object defining each option, options can also be provided as
148+
a function (or async function) which returns this options object. This function
149+
is provided the arguments `(request, response, graphQLParams)` and is called
150+
after the request has been parsed.
151+
152+
The `graphQLParams` is provided as the object `{ query, variables, operationName, raw }`.
153+
154+
```js
155+
app.use(
156+
mount(
157+
'/graphql',
158+
graphqlHTTP(async (request, response, ctx, graphQLParams) => ({
159+
schema: MyGraphQLSchema,
160+
rootValue: await someFunctionToGetRootValue(request),
161+
graphiql: true,
162+
})),
163+
),
164+
);
165+
```
166+
147167
## HTTP Usage
148168

149169
Once installed at a path, `koa-graphql` will accept requests with
@@ -336,13 +356,15 @@ package.
336356
import { NoSchemaIntrospectionCustomRule } from 'graphql';
337357

338358
app.use(
339-
'/graphql',
340-
graphqlHTTP((request) => {
341-
return {
342-
schema: MyGraphQLSchema,
343-
validationRules: [NoSchemaIntrospectionCustomRule],
344-
};
345-
}),
359+
mount(
360+
'/graphql',
361+
graphqlHTTP((request) => {
362+
return {
363+
schema: MyGraphQLSchema,
364+
validationRules: [NoSchemaIntrospectionCustomRule],
365+
};
366+
}),
367+
),
346368
);
347369
```
348370

src/__tests__/http-test.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,46 @@ describe('GraphQL-HTTP tests', () => {
545545
expect(response.text).to.equal('{"data":{"test":"Hello World"}}');
546546
});
547547

548+
it('Provides an options function with arguments', async () => {
549+
const app = server();
550+
551+
let seenRequest;
552+
let seenResponse;
553+
let seenContext;
554+
let seenParams;
555+
556+
app.use(
557+
mount(
558+
urlString(),
559+
graphqlHTTP((req, res, ctx, params) => {
560+
seenRequest = req;
561+
seenResponse = res;
562+
seenContext = ctx;
563+
seenParams = params;
564+
return { schema: TestSchema };
565+
}),
566+
),
567+
);
568+
569+
const response = await request(app.listen()).get(
570+
urlString({
571+
query: '{test}',
572+
}),
573+
);
574+
575+
expect(response.text).to.equal('{"data":{"test":"Hello World"}}');
576+
577+
expect(seenRequest).to.not.equal(null);
578+
expect(seenResponse).to.not.equal(null);
579+
expect(seenContext).to.not.equal(null);
580+
expect(seenParams).to.deep.equal({
581+
query: '{test}',
582+
operationName: null,
583+
variables: null,
584+
raw: false,
585+
});
586+
});
587+
548588
it('Catches errors thrown from options function', async () => {
549589
const app = server();
550590

@@ -965,7 +1005,7 @@ describe('GraphQL-HTTP tests', () => {
9651005
app.use(
9661006
mount(
9671007
urlString(),
968-
graphqlHTTP((_req, ctx) => {
1008+
graphqlHTTP((_req, _res, ctx) => {
9691009
expect(ctx.req.file?.originalname).to.equal('test.txt');
9701010
return {
9711011
schema: TestMutationSchema,
@@ -1168,10 +1208,10 @@ describe('GraphQL-HTTP tests', () => {
11681208
app.use(
11691209
mount(
11701210
urlString(),
1171-
graphqlHTTP((reqest, response, context) => {
1172-
seenRequest = reqest;
1173-
seenResponse = response;
1174-
seenContext = context;
1211+
graphqlHTTP((req, res, ctx) => {
1212+
seenRequest = req;
1213+
seenResponse = res;
1214+
seenContext = ctx;
11751215
return { schema: TestSchema };
11761216
}),
11771217
),

0 commit comments

Comments
 (0)