Skip to content

Commit

Permalink
Add support for connect (#58)
Browse files Browse the repository at this point in the history
* add support for connect

* export apolloConnect and graphiqlConnect

* update changelog
  • Loading branch information
helfer authored Jul 29, 2016
1 parent 61ded20 commit 1f2d6ca
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Added `formatRequest` and `formatResponse` functions to apollo options.
* Removed support for shorthand schema definitions, connectors and mocks (use `graphql-tools` instead)
* Added Koa integration (@HriBB in #59)
* Changed express integration to support connect as well (@helfer in #58)


### v0.1.5
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"babel-preset-es2015": "^6.9.0",
"body-parser": "^1.15.2",
"chai": "^3.5.0",
"connect": "^3.4.1",
"istanbul": "1.0.0-alpha.2",
"koa-bodyparser": "^3.0.0",
"koa-router": "^7.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { apolloExpress, graphiqlExpress } from './integrations/expressApollo';
export { ApolloHAPI, GraphiQLHAPI } from './integrations/hapiApollo';
export { apolloKoa } from './integrations/koaApollo';
export { apolloConnect, graphiqlConnect } from './integrations/connectApollo';
23 changes: 23 additions & 0 deletions src/integrations/connectApollo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as connect from 'connect';
import * as bodyParser from 'body-parser';
import { apolloConnect, graphiqlConnect } from './connectApollo';

import testSuite, { Schema, CreateAppOptions } from './integrations.test';

function createConnectApp(options: CreateAppOptions = {}) {
const app = connect();

options.apolloOptions = options.apolloOptions || { schema: Schema };
if (!options.excludeParser) {
app.use('/graphql', bodyParser.json());
}
if (options.graphiqlOptions ) {
app.use('/graphiql', graphiqlConnect( options.graphiqlOptions ));
}
app.use('/graphql', apolloConnect( options.apolloOptions ));
return app;
}

describe('integration:Connect', () => {
testSuite(createConnectApp);
});
6 changes: 6 additions & 0 deletions src/integrations/connectApollo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { apolloExpress, graphiqlExpress } from './expressApollo';

const apolloConnect = apolloExpress;
const graphiqlConnect = graphiqlExpress;

export { apolloConnect, graphiqlConnect };
34 changes: 20 additions & 14 deletions src/integrations/expressApollo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as express from 'express';
import * as graphql from 'graphql';
import * as url from 'url';
import { runQuery } from '../core/runQuery';

import ApolloOptions from './apolloOptions';
Expand Down Expand Up @@ -34,8 +35,9 @@ export function apolloExpress(options: ApolloOptions | ExpressApolloOptionsFunct
try {
optionsObject = await options(req);
} catch (e) {
res.status(500);
res.send(`Invalid options provided to ApolloServer: ${e.message}`);
res.statusCode = 500;
res.write(`Invalid options provided to ApolloServer: ${e.message}`);
res.end();
}
} else {
optionsObject = options;
Expand All @@ -45,14 +47,16 @@ export function apolloExpress(options: ApolloOptions | ExpressApolloOptionsFunct

if (req.method !== 'POST') {
res.setHeader('Allow', 'POST');
res.status(405);
res.send('Apollo Server supports only POST requests.');
res.statusCode = 405;
res.write('Apollo Server supports only POST requests.');
res.end();
return;
}

if (!req.body) {
res.status(500);
res.send('POST body missing. Did you forget "app.use(bodyParser.json())"?');
res.statusCode = 500;
res.write('POST body missing. Did you forget "app.use(bodyParser.json())"?');
res.end();
return;
}

Expand Down Expand Up @@ -100,15 +104,17 @@ export function apolloExpress(options: ApolloOptions | ExpressApolloOptionsFunct
}
}

res.set('Content-Type', 'application/json');
res.setHeader('Content-Type', 'application/json');
if (isBatch) {
res.send(JSON.stringify(responses));
res.write(JSON.stringify(responses));
res.end();
} else {
const gqlResponse = responses[0];
if (gqlResponse.errors && typeof gqlResponse.data === 'undefined') {
res.status(400);
res.statusCode = 400;
}
res.send(JSON.stringify(gqlResponse));
res.write(JSON.stringify(gqlResponse));
res.end();
}

};
Expand All @@ -131,8 +137,7 @@ function isOptionsFunction(arg: ApolloOptions | ExpressApolloOptionsFunction): a

export function graphiqlExpress(options: GraphiQL.GraphiQLData) {
return (req: express.Request, res: express.Response, next) => {

const q = req.query || {};
const q = req.url && url.parse(req.url, true).query || {};
const query = q.query || '';
const variables = q.variables || '{}';
const operationName = q.operationName || '';
Expand All @@ -144,7 +149,8 @@ export function graphiqlExpress(options: GraphiQL.GraphiQLData) {
variables: JSON.parse(variables) || options.variables,
operationName: operationName || options.operationName,
});
res.set('Content-Type', 'text/html');
res.send(graphiQLString);
res.setHeader('Content-Type', 'text/html');
res.write(graphiQLString);
res.end();
};
}
1 change: 1 addition & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require('source-map-support').install();
import '../core/runQuery.test';
import '../modules/operationStore.test';
import '../integrations/expressApollo.test';
import '../integrations/connectApollo.test';
import '../integrations/hapiApollo.test';
import '../integrations/koaApollo.test';
import './testApolloServerHTTP';
1 change: 1 addition & 0 deletions typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"globalDependencies": {
"body-parser": "registry:dt/body-parser#0.0.0+20160619023215",
"cookies": "registry:dt/cookies#0.5.1+20160316171810",
"connect": "registry:dt/connect#3.4.0+20160317120654",
"express": "registry:dt/express#4.0.0+20160708185218",
"express-serve-static-core": "registry:dt/express-serve-static-core#4.0.0+20160715232503",
"hapi": "registry:dt/hapi#13.0.0+20160709092105",
Expand Down

0 comments on commit 1f2d6ca

Please sign in to comment.