Skip to content

Commit

Permalink
fix: add back shallow cloning, update options to accept context as a …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
bennyhobart committed Dec 8, 2017
1 parent 3fe6303 commit 24a60a0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
### vNEXT
* Added support for the vhost option for Hapi [PR #611](https://github.com/apollographql/apollo-server/pull/611)
* Include readme for npm packages
* Remove context cloning when batching[PR #679](https://github.com/apollographql/apollo-server/pull/679)
* Update peerDependencies version for micro [PR #671](https://github.com/apollographql/apollo-server/pull/671)
* Change GraphqlOptions to also accept context as a function[PR #679](https://github.com/apollographql/apollo-server/pull/679)

### v1.1.6
* Fixes bug where CORS would not allow `Access-Control-Allow-Origin: *` with credential 'include', changed to 'same-origin' [Issue #514](https://github.com/apollographql/apollo-server/issues/514)
Expand Down
13 changes: 12 additions & 1 deletion packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function isQueryOperation(query: DocumentNode, operationName: string) {
return operationAST.operation === 'query';
}

export function isFunction(arg: any): arg is Function {
return typeof arg === 'function';
}

export async function runHttpQuery(handlerArguments: Array<any>, request: HttpQueryRequest): Promise<string> {
let isGetRequest: boolean = false;
let optionsObject: GraphQLOptions;
Expand Down Expand Up @@ -97,11 +101,18 @@ export async function runHttpQuery(handlerArguments: Array<any>, request: HttpQu
}
}

let context = optionsObject.context;
if (isFunction(context)) {
context = context();
} else if (isBatch) {
context = Object.assign({}, context || {});
}

let params = {
schema: optionsObject.schema,
query: query,
variables: variables,
context: optionsObject.context,
context,
rootValue: optionsObject.rootValue,
operationName: operationName,
logFunction: optionsObject.logFunction,
Expand Down
71 changes: 70 additions & 1 deletion packages/apollo-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const queryType = new GraphQLObjectType({
testContext: {
type: GraphQLString,
resolve(_, args, context) {
return context.testField;
if (context.otherField) {
return 'unexpected';
}
context.otherField = true;
return context.testField;
},
},
testRootValue: {
Expand Down Expand Up @@ -487,6 +491,71 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
});
});

it('clones batch context', () => {
app = createApp({graphqlOptions: {
schema,
context: {testField: 'expected'},
}});
const expected = [
{
data: {
testContext: 'expected',
},
},
{
data: {
testContext: 'expected',
},
},
];
const req = request(app)
.post('/graphql')
.send([{
query: 'query test{ testContext }',
}, {
query: 'query test{ testContext }',
}]);
return req.then((res) => {
expect(res.status).to.equal(200);
return expect(res.body).to.deep.equal(expected);
});
});

it('executes batch context if it is a function', () => {
let callCount = 0;
app = createApp({graphqlOptions: {
schema,
context: () => {
callCount++;
return ({testField: 'expected'});
},
}});
const expected = [
{
data: {
testContext: 'expected',
},
},
{
data: {
testContext: 'expected',
},
},
];
const req = request(app)
.post('/graphql')
.send([{
query: 'query test{ testContext }',
}, {
query: 'query test{ testContext }',
}]);
return req.then((res) => {
expect(callCount).to.equal(2);
expect(res.status).to.equal(200);
return expect(res.body).to.deep.equal(expected);
});
});

it('can handle a request with a mutation', () => {
app = createApp();
const expected = {
Expand Down

0 comments on commit 24a60a0

Please sign in to comment.