Skip to content

Commit

Permalink
Merge 342fca7 into 36ae6d4
Browse files Browse the repository at this point in the history
  • Loading branch information
n1mmy committed Oct 19, 2016
2 parents 36ae6d4 + 342fca7 commit da010c2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 7 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
13 changes: 12 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,20 @@ 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.
let context = optionsObject.context;
if (isBatch) {
context = Object.assign({}, 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
17 changes: 14 additions & 3 deletions src/integrations/hapiApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const apolloHapi: IRegister = function(server: Server, options: HapiPluginOption
method: 'getApolloOptions',
}, {
assign: 'graphQL',
method: 'processQuery(pre.graphqlParams, pre.apolloOptions)',
method: 'processQuery(pre.graphqlParams, pre.apolloOptions, pre.isBatch)',
}],
});

Expand Down Expand Up @@ -124,18 +124,29 @@ async function getApolloOptions(request: Request, reply: IReply): Promise<{}> {
reply(optionsObject);
}

async function processQuery(graphqlParams, optionsObject: ApolloOptions, reply) {
async function processQuery(graphqlParams, optionsObject: ApolloOptions, isBatch: boolean, 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.
let context = optionsObject.context;
if (isBatch) {
context = Object.assign({}, 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
80 changes: 78 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,61 @@ 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('does not put batch index in context', () => {
app = createApp();
const expected = {
data: {
testBatchIndex: null,
testBatchSize: null,
},
};
const req = request(app)
.post('/graphql')
.send({
query: `query test1 { testBatchIndex, testBatchSize }`,
});
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 +449,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
13 changes: 12 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,20 @@ 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.
let context = optionsObject.context;
if (isBatch) {
context = Object.assign({}, 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 da010c2

Please sign in to comment.