Skip to content

Commit

Permalink
feat(package): allow custom context
Browse files Browse the repository at this point in the history
  • Loading branch information
I committed Jul 26, 2016
1 parent 043e3a9 commit 0df66ae
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# ![graffiti](https://cloud.githubusercontent.com/assets/1764512/8900273/9ed758dc-343e-11e5-95ba-e82f876cf52d.png)

[![npm version](https://badge.fury.io/js/%40risingstack%2Fgraffiti.svg)](https://badge.fury.io/js/%40risingstack%2Fgraffiti)
[ ![Codeship Status for RisingStack/graffiti](https://codeship.com/projects/0c4fb010-5969-0133-8c37-4255fd5efb39/status?branch=master)](https://codeship.com/projects/110029)
[![bitHound Overall Score](https://www.bithound.io/github/RisingStack/graffiti/badges/score.svg)](https://www.bithound.io/github/RisingStack/graffiti)
[![Known Vulnerabilities](https://snyk.io/test/npm/@risingstack/graffiti/badge.svg)](https://snyk.io/test/npm/@risingstack/graffiti)

Currently the consumption of HTTP REST APIs dominate the client-side world,
[GraphQL](https://github.com/facebook/graphql) aims to change this.
Expand All @@ -26,7 +28,7 @@ For a running **example server** and **executable queries**, check out our examp
## Adapters

* [mongoose](https://github.com/RisingStack/graffiti-mongoose)
* more coming soon...
* more coming...

## Supported servers

Expand Down Expand Up @@ -64,7 +66,8 @@ const app = express();
app.use(json());

app.use(graffiti.express({
schema: getSchema([User, Cat])
schema: getSchema([User, Cat]),
context: {} // custom context
}));

app.listen(3000);
Expand All @@ -83,7 +86,8 @@ server.connection({ port: 3000 });
server.register({
register: graffiti.hapi,
options: {
schema: getSchema([User, Cat])
schema: getSchema([User, Cat]),
context: {} // custom context
}
}, function (err) {
if (err) {
Expand Down Expand Up @@ -112,7 +116,8 @@ const app = koa();
app.use(parser());

app.use(graffiti.koa({
schema: getSchema([User, Cat])
schema: getSchema([User, Cat]),
context: {} // custom context
}));

app.listen(3000);
Expand All @@ -122,6 +127,7 @@ app.listen(3000);

- `schema`: a `GraphQLSchema` instance. You can use an adapters `getSchema` method, or provide your own schema. (required)
- `graphiql`: may present [GraphiQL](https://github.com/graphql/graphiql) when loaded directly from a browser. (default: `true`)
- `context`: custom GraphQL context object. (default: `{}`)

## Test

Expand Down
4 changes: 2 additions & 2 deletions src/express/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function sendError(response, boom) {
response.status(statusCode).send(payload);
}

export default function middleware({ graphiql = true, schema = required() } = {}) {
export default function middleware({ graphiql = true, context = {}, schema = required() } = {}) {
return (request, response, next) => {
if (isPath(request) && (isPost(request) || isGet(request))) {
const body = request.body;
Expand All @@ -38,7 +38,7 @@ export default function middleware({ graphiql = true, schema = required() } = {}
// ignore
}

return graphql(schema, query, request, request, parsedVariables)
return graphql(schema, query, context, request, parsedVariables)
.then((result) => {
if (result.errors) {
const message = result.errors.map((error) => error.message).join('\n');
Expand Down
4 changes: 2 additions & 2 deletions src/hapi/hapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function accepts({ headers }, type) {
}

const plugin = {
register: (server, { graphiql = true, schema = required() } = {}, next) => {
register: (server, { graphiql = true, context = {}, schema = required() } = {}, next) => {
const handler = (request, reply) => {
const data = request.payload || request.query || {};
const { query, variables } = data;
Expand All @@ -36,7 +36,7 @@ const plugin = {
// ignore
}

return graphql(schema, query, request, request, parsedVariables)
return graphql(schema, query, context, request, parsedVariables)
.then((result) => {
if (result.errors) {
const message = result.errors.map((error) => error.message).join('\n');
Expand Down
4 changes: 2 additions & 2 deletions src/koa/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function accepts(type) {
return this.headers && this.headers.accept && this.headers.accept.includes(type);
}

export default function middleware({ graphiql = true, schema = required() } = {}) {
export default function middleware({ graphiql = true, context = {}, schema = required() } = {}) {
return function *middleware(next) {
if (isPath(this) && (isPost(this) || isGet(this))) {
const body = this.request.body;
Expand All @@ -33,7 +33,7 @@ export default function middleware({ graphiql = true, schema = required() } = {}
// ignore
}

this.body = yield graphql(schema, query, this, this, parsedVariables);
this.body = yield graphql(schema, query, context, this, parsedVariables);
return this.body;
}

Expand Down

0 comments on commit 0df66ae

Please sign in to comment.