forked from apollographql/subscriptions-transport-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apollographql#28 from apollostack/core-refactor-ex…
…press Core refactor express
- Loading branch information
Showing
9 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import expressApollo from './integrations/expressApollo'; | ||
|
||
export { expressApollo }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters