Skip to content

Commit

Permalink
Merge pull request apollographql#28 from apollostack/core-refactor-ex…
Browse files Browse the repository at this point in the history
…press

Core refactor express
  • Loading branch information
helfer authored Jun 14, 2016
2 parents dfe30e3 + fcf1376 commit 6382656
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 1 deletion.
31 changes: 31 additions & 0 deletions integrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# expressApollo

An Express Middleware for the Apollo Server

## Example Usage

```js
import * as express from "express";
import * as bodyParser from "body-parser";
import { expressApollo } from "apollo-server";
import schema from "./data/schema";
import * as graphql from 'graphql'

const port = 3000;
const app = express();
const schema = new graphql.GraphQLSchema({
query: new graphql.GraphQLObjectType({
name: 'Query',
fields: {
testString: { type: graphql.GraphQLString }
}
})
});

app.use(bodyParser.text());
app.use("/", expressApollo({schema}));

app.listen(port, () => {
console.log(`Server is listen on ${port}`);
});
```
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"homepage": "https://github.com/apollostack/apollo-proxy#readme",
"dependencies": {
"es6-promise": "^3.2.1",
"express": "^4.13.4",
"graphql": "^0.6.0",
"source-map-support": "^0.4.0"
},
Expand All @@ -56,5 +57,9 @@
},
"peerDependencies": {
"graphql": "^0.6.0"
},
"typings": "dist/index.d.ts",
"typescript": {
"definition": "dist/index.d.ts"
}
}
2 changes: 2 additions & 0 deletions src/core/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
execute,
} from 'graphql';

import { Promise } from 'es6-promise';

export interface GqlResponse {
data?: Object;
errors?: Array<string>;
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import expressApollo from './integrations/expressApollo';

export { expressApollo };
43 changes: 43 additions & 0 deletions src/integrations/expressApollo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
assert,
} from 'chai';

// XXX can be removed after tests are actually writen
/* tslint:disable:no-unused-variable */
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
} from 'graphql';

import expressApollo from './expressApollo';

const QueryType = new GraphQLObjectType({
name: 'QueryType',
fields: {
testString: {
type: GraphQLString,
resolve() {
return 'it works';
},
},
},
});

const Schema = new GraphQLSchema({
query: QueryType,
});
// XXX can be removed after tests are actually writen
/* tslint:enable:no-unused-variable */

describe('expressApollo', () => {
it('returns express middleware', () => {
// XXX can be removed after tests are actually writen
// tslint:disable-next-line:no-unused-variable
const query = `{ testString }`;
const middleware = expressApollo({
schema: Schema,
});
assert(typeof middleware === 'function');
});
});
29 changes: 29 additions & 0 deletions src/integrations/expressApollo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as express from 'express';
import * as graphql from 'graphql';
import { runQuery } from '../core/runQuery';

export interface ExpressBindings {
schema: graphql.GraphQLSchema;
}

export default function(options: ExpressBindings) {
if (!options) {
throw new Error('GraphQL middleware requires options.');
}

if (arguments.length > 1) {
throw new Error(`apolloServer expects exactly one argument, got ${arguments.length + 1}`);
}

return (req: express.Request, res: express.Response, next) => {
runQuery({
schema: options.schema,
query: req.body,
}).then(gqlResponse => {
res.set('Content-Type', 'application/json');
res.send({ data: gqlResponse.data });
}).catch(gqlResponse => {
res.send(gqlResponse.errorCode, { errors: gqlResponse.errors });
});
};
}
1 change: 1 addition & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ declare function require(name: string);
require('source-map-support').install();

import '../core/runQuery.test';
import '../integrations/expressApollo.test';
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
Expand Down
1 change: 1 addition & 0 deletions typings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"graphql": "github:nitintutlani/typed-graphql"
},
"globalDependencies": {
"es6-promise": "registry:dt/es6-promise#0.0.0+20160317120654",
"body-parser": "registry:dt/body-parser#0.0.0+20160317120654",
"express": "registry:dt/express#4.0.0+20160317120654",
"express-serve-static-core": "registry:dt/express-serve-static-core#0.0.0+20160322035842",
Expand Down

0 comments on commit 6382656

Please sign in to comment.