Skip to content

Commit

Permalink
Merge f56afb2 into 36ae6d4
Browse files Browse the repository at this point in the history
  • Loading branch information
n1mmy committed Oct 19, 2016
2 parents 36ae6d4 + f56afb2 commit 4de66a8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Manage TypeScript declaration files using npm. ([@od1k](https:/github.com/od1k) in [#162](https://github.com/apollostack/apollo-server/pull/162))
* Fix connect example in readme. ([@conrad-vanl](https://github.com/conrad-vanl) in [#165](https://github.com/apollostack/apollo-server/pull/165))
* Add try/catch to formatError. ([@nicolaslopezj](https://github.com/nicolaslopezj) in [#174](https://github.com/apollostack/apollo-server/pull/174))
* Add `apolloBatchIndex` and `apolloBatchSize` fields to the `context` object to let users detect when batching is requested.

### v0.3.2
* Added missing exports for hapi integration ([@nnance](https://github.com/nnance)) in [PR #152](https://github.com/apollostack/apollo-server/pull/152)
Expand Down
10 changes: 9 additions & 1 deletion src/integrations/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ export function apolloExpress(options: ApolloOptions | ExpressApolloOptionsFunct
}

let responses: Array<graphql.GraphQLResult> = [];
let batchIndex: number = -1;
for (let requestParams of b) {
try {
const query = requestParams.query;
const operationName = requestParams.operationName;
let variables = requestParams.variables;
batchIndex += 1;

if (typeof variables === 'string') {
try {
Expand All @@ -87,11 +89,17 @@ export function apolloExpress(options: ApolloOptions | ExpressApolloOptionsFunct
}
}

// shallow clone the context object to put batch markers in.
// create a context object if there isn't one passed in.
const context = Object.assign({}, optionsObject.context || {});
context.apolloBatchIndex = batchIndex;
context.apolloBatchSize = b.length;

let params = {
schema: optionsObject.schema,
query: query,
variables: variables,
context: optionsObject.context,
context: context,
rootValue: optionsObject.rootValue,
operationName: operationName,
logFunction: optionsObject.logFunction,
Expand Down
10 changes: 9 additions & 1 deletion src/integrations/hapiApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,22 @@ async function processQuery(graphqlParams, optionsObject: ApolloOptions, reply)
const formatErrorFn = optionsObject.formatError || formatError;

let responses: GraphQLResult[] = [];
let batchIndex: number = -1;
for (let query of graphqlParams) {
batchIndex += 1;
try {
// shallow clone the context object to put batch markers in.
// create a context object if there isn't one passed in.
const context = Object.assign({}, optionsObject.context || {});
context.apolloBatchIndex = batchIndex;
context.apolloBatchSize = graphqlParams.length;

let params = {
schema: optionsObject.schema,
query: query.query,
variables: query.variables,
rootValue: optionsObject.rootValue,
context: optionsObject.context,
context: context,
operationName: query.operationName,
logFunction: optionsObject.logFunction,
validationRules: optionsObject.validationRules,
Expand Down
61 changes: 59 additions & 2 deletions src/integrations/integrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLError,
introspectionQuery,
BREAK,
Expand All @@ -30,7 +31,7 @@ const QueryType = new GraphQLObjectType({
testContext: {
type: GraphQLString,
resolve(_, args, context) {
return context;
return context.testField;
},
},
testRootValue: {
Expand All @@ -46,6 +47,26 @@ const QueryType = new GraphQLObjectType({
return `hello ${echo}`;
},
},
testBatchIndex: {
type: GraphQLInt,
resolve(root, _, context) {
if (context) {
return context.apolloBatchIndex;
} else {
return null;
}
},
},
testBatchSize: {
type: GraphQLInt,
resolve(root, _, context) {
if (context) {
return context.apolloBatchSize;
} else {
return null;
}
},
},
testError: {
type: GraphQLString,
resolve() {
Expand Down Expand Up @@ -332,6 +353,42 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
});
});

it('puts batch index in context', () => {
app = createApp();
const expected = [
{
data: {
testBatchIndex: 0,
testBatchSize: 3,
},
},
{
data: {
testString : 'it works',
},
},
{
data: {
testBatchIndex: 2,
},
},
];
const req = request(app)
.post('/graphql')
.send([{
query: `query test1 { testBatchIndex, testBatchSize }`,
}, {
query: `query test2 { testString }`,
}, {
query: `query test3 { testBatchIndex }`,
}]);
return req.then((res) => {
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 Expand Up @@ -373,7 +430,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
const expected = 'context works';
app = createApp({apolloOptions: {
schema: Schema,
context: expected,
context: {testField: expected},
}});
const req = request(app)
.post('/graphql')
Expand Down
10 changes: 9 additions & 1 deletion src/integrations/koaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ export function apolloKoa(options: ApolloOptions | KoaApolloOptionsFunction): Ko
}

let responses: Array<graphql.GraphQLResult> = [];
let batchIndex: number = -1;
for (let requestParams of b) {
try {
const query = requestParams.query;
const operationName = requestParams.operationName;
let variables = requestParams.variables;
batchIndex += 1;

if (typeof variables === 'string') {
try {
Expand All @@ -64,11 +66,17 @@ export function apolloKoa(options: ApolloOptions | KoaApolloOptionsFunction): Ko
}
}

// shallow clone the context object to put batch markers in.
// create a context object if there isn't one passed in.
const context = Object.assign({}, optionsObject.context || {});
context.apolloBatchIndex = batchIndex;
context.apolloBatchSize = b.length;

let params = {
schema: optionsObject.schema,
query: query,
variables: variables,
context: optionsObject.context,
context: context,
rootValue: optionsObject.rootValue,
operationName: operationName,
logFunction: optionsObject.logFunction,
Expand Down

0 comments on commit 4de66a8

Please sign in to comment.