Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

feature(dynamic-context): Execute function for context #394

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 79 additions & 26 deletions src/__tests__/http-test.js
Expand Up @@ -553,12 +553,10 @@ describe('test harness', () => {
}),
);

const response = await request(app)
.post(urlString())
.send({
query: 'query helloWho($who: String){ test(who: $who) }',
variables: JSON.stringify({ who: 'Dolly' }),
});
const response = await request(app).post(urlString()).send({
query: 'query helloWho($who: String){ test(who: $who) }',
variables: JSON.stringify({ who: 'Dolly' }),
});

expect(response.text).to.equal('{"data":{"test":"Hello Dolly"}}');
});
Expand All @@ -574,12 +572,10 @@ describe('test harness', () => {
}),
);

const response = await request(app)
.post(urlString())
.send({
query: 'query helloWho($who: String){ test(who: $who) }',
variables: { who: 'Dolly' },
});
const response = await request(app).post(urlString()).send({
query: 'query helloWho($who: String){ test(who: $who) }',
variables: { who: 'Dolly' },
});

expect(response.text).to.equal('{"data":{"test":"Hello Dolly"}}');
});
Expand All @@ -595,14 +591,12 @@ describe('test harness', () => {
}),
);

const response = await request(app)
.post(urlString())
.send(
stringify({
query: 'query helloWho($who: String){ test(who: $who) }',
variables: JSON.stringify({ who: 'Dolly' }),
}),
);
const response = await request(app).post(urlString()).send(
stringify({
query: 'query helloWho($who: String){ test(who: $who) }',
variables: JSON.stringify({ who: 'Dolly' }),
}),
);

expect(response.text).to.equal('{"data":{"test":"Hello Dolly"}}');
});
Expand Down Expand Up @@ -689,19 +683,17 @@ describe('test harness', () => {
})),
);

const response = await request(app)
.post(urlString())
.send({
query: `
const response = await request(app).post(urlString()).send({
query: `
query helloYou { test(who: "You"), ...shared }
query helloWorld { test(who: "World"), ...shared }
query helloDolly { test(who: "Dolly"), ...shared }
fragment shared on QueryRoot {
shared: test(who: "Everyone")
}
`,
operationName: 'helloWorld',
});
operationName: 'helloWorld',
});

expect(JSON.parse(response.text)).to.deep.equal({
data: {
Expand Down Expand Up @@ -1903,5 +1895,66 @@ describe('test harness', () => {
);
});
});

describe('Custom context capabiility', () => {
it('allows context to be a function', async () => {
const app = server();

get(
app,
urlString(),
graphqlHTTP({
schema: TestSchema,
context: () => Promise.resolve('testValue'),
}),
);

const response = await request(app).get(
urlString({
operationName: 'TestQuery',
query: `
query TestQuery { context }
`,
}),
);

expect(response.status).to.equal(200);
expect(JSON.parse(response.text)).to.deep.equal({
data: {
context: 'testValue',
},
});
});

it('correctly handles errors in context function', async () => {
const app = server();

get(
app,
urlString(),
graphqlHTTP({
schema: TestSchema,
context: () =>
new Promise(() => {
throw Object.assign(
{},
{ status: 500, message: 'Context function error.' },
);
}),
}),
);

const response = await request(app).get(
urlString({
operationName: 'TestQuery',
query: `
query TestQuery { context }
`,
}),
);

expect(response.status).to.equal(500);
});
});
});
});
44 changes: 31 additions & 13 deletions src/index.js
Expand Up @@ -247,21 +247,39 @@ function graphqlHTTP(options: Options): Middleware {
);
}
}
// Perform the execution, reporting any errors creating the context.
try {
return execute(
schema,
documentAST,
rootValue,
context,
variables,
operationName,

let preExecute;
if (typeof context === 'function') {
preExecute = Promise.resolve().then(() =>
context(
request,
response,
schema,
documentAST,
variables,
operationName,
),
);
} catch (contextError) {
// Return 400: Bad Request if any execution context errors exist.
response.statusCode = 400;
return { errors: [contextError] };
} else {
preExecute = Promise.resolve(context);
}
// Perform the execution, reporting any errors creating the context.
return preExecute
.then(dynamicContext => {
return execute(
schema,
documentAST,
rootValue,
dynamicContext,
variables,
operationName,
);
})
.catch(contextError => {
// Return 400: Bad Request if any execution context errors exist.
response.statusCode = (contextError || {}).status || 400;
return { errors: [contextError] };
});
})
.then(result => {
// Collect and apply any metadata extensions if a function was provided.
Expand Down