koa-graphql
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -20,2 +20,4 @@ 'use strict'; | ||
var _renderGraphiQL = require('./renderGraphiQL'); | ||
var _bluebird = require('bluebird'); | ||
@@ -54,2 +56,3 @@ | ||
var pretty = _getOptions.pretty; | ||
var graphiql = _getOptions.graphiql; | ||
@@ -75,2 +78,35 @@ // GraphQL HTTP only supports GET and POST methods. | ||
var operationName = _getGraphQLParams.operationName; | ||
// If there is no query, present an empty GraphiQL if possible, otherwise | ||
// return a 400 level error. | ||
if (!query) { | ||
if (graphiql && canDisplayGraphiQL(request, data)) { | ||
response.type = 'text/html'; | ||
response.body = (0, _renderGraphiQL.renderGraphiQL)(); | ||
return; | ||
} | ||
throw (0, _httpErrors2['default'])(400, 'Must provide query string.'); | ||
} | ||
// Run GraphQL query. | ||
var result = yield (0, _graphql.graphql)(schema, query, rootValue, variables, operationName); | ||
// Format any encountered errors. | ||
if (result.errors) { | ||
result.errors = result.errors.map(_graphqlError.formatError); | ||
} | ||
// Report 200:Success if a data key exists, | ||
// Otherwise 400:BadRequest if only errors exist. | ||
response.status = result.hasOwnProperty('data') ? 200 : 400; | ||
// If allowed to show GraphiQL, present it instead of JSON. | ||
if (graphiql && canDisplayGraphiQL(request, data)) { | ||
response.type = 'text/html'; | ||
response.body = (0, _renderGraphiQL.renderGraphiQL)({ query: query, variables: variables, result: result }); | ||
} else { | ||
// Otherwise, present JSON directly. | ||
response.type = 'application/json'; | ||
response.body = JSON.stringify(result, null, pretty ? 2 : 0); | ||
} | ||
} catch (error) { | ||
@@ -80,16 +116,2 @@ // Format any request errors the same as GraphQL errors. | ||
} | ||
// Run GraphQL query. | ||
var result = yield (0, _graphql.graphql)(schema, query, rootValue, variables, operationName); | ||
// Format any encountered errors. | ||
if (result.errors) { | ||
result.errors = result.errors.map(_graphqlError.formatError); | ||
} | ||
// Report 200:Success if a data key exists, | ||
// Otherwise 400:BadRequest if only errors exist. | ||
response.status = result.hasOwnProperty('data') ? 200 : 400; | ||
response.type = 'application/json'; | ||
response.body = JSON.stringify(result, null, pretty ? 2 : 0); | ||
}; | ||
@@ -122,5 +144,2 @@ } | ||
var query = request.query.query || data.query; | ||
if (!query) { | ||
throw (0, _httpErrors2['default'])(400, 'Must provide query string.'); | ||
} | ||
@@ -144,2 +163,13 @@ // Parse the variables if needed. | ||
/** | ||
* Helper function to determine if GraphiQL can be displayed. | ||
*/ | ||
function canDisplayGraphiQL(request, data) { | ||
// If `raw` exists, GraphiQL mode is not enabled. | ||
var raw = request.query.raw !== undefined || data.raw !== undefined; | ||
// Allowed to show GraphiQL if not requested as raw and this request | ||
// prefers HTML over JSON. | ||
return !raw && request.accepts(['json', 'html']) === 'html'; | ||
} | ||
/** | ||
* Helper for formatting errors | ||
@@ -165,2 +195,6 @@ */ | ||
* A boolean to configure whether the output should be pretty-printed. | ||
*/ | ||
/** | ||
* A boolean to optionally enable GraphiQL mode | ||
*/ |
{ | ||
"name": "koa-graphql", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Create a GraphQL HTTP server with Koa.", | ||
@@ -5,0 +5,0 @@ "contributors": [ |
@@ -27,3 +27,3 @@ # GraphQL Koa Middleware | ||
app.use(mount('/graphql', graphqlHTTP({ schema: MyGraphQLSchema }))); | ||
app.use(mount('/graphql', graphqlHTTP({ schema: MyGraphQLSchema, graphiql: true }))); | ||
``` | ||
@@ -33,2 +33,17 @@ | ||
### Options | ||
The `graphqlHTTP` function accepts the following options: | ||
* **`schema`**: A `GraphQLSchema` instance from [`graphql-js`][]. | ||
A `schema` *must* be provided. | ||
* **`rootValue`**: A value to pass as the rootValue to the `graphql()` | ||
function from [`graphql-js`][]. | ||
* **`pretty`**: If `true`, any JSON response will be pretty-printed. | ||
* **`graphiql`**: If `true`, may present [GraphiQL][] when loaded directly | ||
from a browser (a useful tool for debugging and exploration). | ||
### HTTP Usage | ||
@@ -49,2 +64,6 @@ | ||
* **`raw`**: If the `graphiql` option is enabled and the `raw` parameter is | ||
provided raw JSON will always be returned instead of GraphiQL even when | ||
loaded from a browser. | ||
GraphQL will first look for each parameter in the URL's query-string: | ||
@@ -84,6 +103,7 @@ | ||
[`graphql-js`]: https://github.com/graphql/graphql-js | ||
[GraphiQL]: https://github.com/graphql/graphiql | ||
[`multer`]: https://github.com/expressjs/multer | ||
[npm-image]: https://img.shields.io/npm/v/koa-graphql.svg?style=flat-square | ||
[npm-url]: https://npmjs.org/package/koa-graphql | ||
[travis-image]: https://travis-ci.org/chentsulin/koa-graphql.svg | ||
[travis-image]: https://travis-ci.org/chentsulin/koa-graphql.svg?branch=master | ||
[travis-url]: https://travis-ci.org/chentsulin/koa-graphql | ||
@@ -90,0 +110,0 @@ [coveralls-image]: https://coveralls.io/repos/chentsulin/koa-graphql/badge.svg?branch=master&service=github |
23491
7
293
110