Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-graphql

Package Overview
Dependencies
Maintainers
3
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-graphql - npm Package Compare versions

Comparing version 0.6.4 to 0.6.5

dist/index.js.flow

71

dist/index.js

@@ -60,7 +60,4 @@ 'use strict';

// asynchronous state machine below.
var schema = void 0;
var context = void 0;
var rootValue = void 0;
var params = void 0;
var pretty = void 0;
var graphiql = void 0;
var formatErrorFn = void 0;

@@ -70,6 +67,6 @@ var extensionsFn = void 0;

var query = void 0;
var documentAST = void 0;
var variables = void 0;
var operationName = void 0;
var validationRules = void 0;

@@ -79,5 +76,9 @@ // Promises are used as a mechanism for capturing any thrown errors during

// Resolve the Options to get OptionsData.
return new Promise(function (resolve) {
resolve(typeof options === 'function' ? options(request, response) : options);
// Parse the Request to get GraphQL request parameters.
return getGraphQLParams(request).then(function (graphQLParams) {
params = graphQLParams;
// Then, resolve the Options to get OptionsData.
return new Promise(function (resolve) {
return resolve(typeof options === 'function' ? options(request, response, params) : options);
});
}).then(function (optionsData) {

@@ -95,11 +96,11 @@ // Assert that optionsData is in fact an Object.

// Collect information from the options data object.
schema = optionsData.schema;
context = optionsData.context || request;
rootValue = optionsData.rootValue;
var schema = optionsData.schema;
var context = optionsData.context || request;
var rootValue = optionsData.rootValue;
var graphiql = optionsData.graphiql;
pretty = optionsData.pretty;
graphiql = optionsData.graphiql;
formatErrorFn = optionsData.formatError;
extensionsFn = optionsData.extensions;
validationRules = _graphql.specifiedRules;
var validationRules = _graphql.specifiedRules;
if (optionsData.validationRules) {

@@ -115,5 +116,2 @@ validationRules = validationRules.concat(optionsData.validationRules);

// Parse the Request to get GraphQL request parameters.
return getGraphQLParams(request);
}).then(function (params) {
// Get GraphQL params from the request and POST body data.

@@ -213,15 +211,28 @@ query = params.query;

}
// If allowed to show GraphiQL, present it instead of JSON.
if (showGraphiQL) {
var payload = (0, _renderGraphiQL.renderGraphiQL)({
query: query, variables: variables,
operationName: operationName, result: result
query: query,
variables: variables,
operationName: operationName,
result: result
});
response.setHeader('Content-Type', 'text/html; charset=utf-8');
sendResponse(response, payload);
return sendResponse(response, 'text/html', payload);
}
// At this point, result is guaranteed to exist, as the only scenario
// where it will not is when showGraphiQL is true.
if (!result) {
throw (0, _httpErrors2.default)(500, 'Internal Error');
}
// If "pretty" JSON isn't requested, and the server provides a
// response.json method (express), use that directly.
// Otherwise use the simplified sendResponse method.
if (!pretty && typeof response.json === 'function') {
response.json(result);
} else {
// Otherwise, present JSON directly.
var _payload = JSON.stringify(result, null, pretty ? 2 : 0);
response.setHeader('Content-Type', 'application/json; charset=utf-8');
sendResponse(response, _payload);
sendResponse(response, 'application/json', _payload);
}

@@ -288,11 +299,9 @@ });

/**
* Helper function for sending the response data. Use response.send it method
* exists (express), otherwise use response.end (connect).
* Helper function for sending a response using only the core Node server APIs.
*/
function sendResponse(response, data) {
if (typeof response.send === 'function') {
response.send(data);
} else {
response.end(data);
}
function sendResponse(response, type, data) {
var chunk = new Buffer(data, 'utf8');
response.setHeader('Content-Type', type + '; charset=utf-8');
response.setHeader('Content-Length', String(chunk.length));
response.end(chunk);
}

@@ -10,3 +10,3 @@ 'use strict';

// Current latest version of GraphiQL.
var GRAPHIQL_VERSION = '0.9.3';
var GRAPHIQL_VERSION = '0.10.1';

@@ -13,0 +13,0 @@ // Ensures string values are safe to be used within a <script> tag.

{
"name": "express-graphql",
"version": "0.6.4",
"version": "0.6.5",
"description": "Production ready GraphQL HTTP middleware.",

@@ -20,2 +20,3 @@ "contributors": [

"express",
"restify",
"connect",

@@ -55,3 +56,4 @@ "http",

"check": "flow check",
"build": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist",
"build": "rm -rf dist/* && babel src --ignore __tests__ --out-dir dist && npm run build:flow",
"build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/dist\\//g'`.flow; done",
"watch": "node resources/watch.js",

@@ -69,33 +71,32 @@ "cover": "babel-node node_modules/.bin/isparta cover --root src --report html node_modules/.bin/_mocha -- $npm_package_options_mocha",

"devDependencies": {
"babel-cli": "6.22.2",
"babel-eslint": "7.2.1",
"babel-plugin-transform-async-to-generator": "6.22.0",
"babel-plugin-transform-class-properties": "6.22.0",
"babel-cli": "6.24.1",
"babel-eslint": "7.2.3",
"babel-plugin-transform-async-to-generator": "6.24.1",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-flow-strip-types": "6.22.0",
"babel-plugin-transform-runtime": "6.22.0",
"babel-preset-es2015": "6.24.0",
"babel-register": "6.24.0",
"babel-preset-es2015": "6.24.1",
"babel-register": "6.24.1",
"babel-runtime": "6.23.0",
"body-parser": "1.17.1",
"chai": "3.5.0",
"connect": "3.6.0",
"coveralls": "2.13.0",
"eslint": "3.18.0",
"eslint-plugin-babel": "4.1.1",
"eslint-plugin-flowtype": "2.30.4",
"connect": "3.6.1",
"coveralls": "2.13.1",
"eslint": "3.19.0",
"eslint-plugin-flowtype": "2.33.0",
"express": "4.14.1",
"express3": "*",
"flow-bin": "0.42.0",
"flow-bin": "0.46.0",
"graphql": "0.9.2",
"isparta": "4.0.0",
"mocha": "3.2.0",
"mocha": "3.4.1",
"multer": "1.3.0",
"restify": "4.3.0",
"sane": "1.6.0",
"sinon": "2.1.0",
"supertest": "3.0.0",
"supertest-as-promised": "4.0.2"
"sinon": "2.2.0",
"supertest": "3.0.0"
},
"peerDependencies": {
"graphql": "^0.5.0-b || ^0.6.0 || ^0.7.0 || ^0.8.0-b || ^0.9.0"
"graphql": "^0.9.6"
}
}

@@ -7,3 +7,3 @@ GraphQL HTTP Server Middleware

Create a GraphQL HTTP server with any HTTP web framework that supports connect styled middleware, including [Connect](https://github.com/senchalabs/connect) itself and [Express](http://expressjs.com).
Create a GraphQL HTTP server with any HTTP web framework that supports connect styled middleware, including [Connect](https://github.com/senchalabs/connect) itself, [Express](http://expressjs.com) and [Restify](http://restify.com/).

@@ -16,4 +16,7 @@ ## Installation

Then mount `express-graphql` as a route handler:
## Simple Setup
Just mount `express-graphql` as a route handler:
```js

@@ -33,2 +36,63 @@ const express = require('express');

## Setup with Restify
In restify the `.use` method is reserved to common handlers so you have to manually register methods (GET, POST, etc) multiple times even for the same endpoint.
If you just need GraphQL with POST requests, just mount `express-graphql` like so:
```js
const restify = require('restify');
const graphqlHTTP = require('express-graphql');
const app = restify.createServer();
app.post('/graphql', graphqlHTTP({
schema: MyGraphQLSchema,
graphiql: false
}));
app.listen(4000);
```
NOTE: Registering GraphQL on POST doesn't allow you to serve GraphiQL since this is an interface accesible through the browser, hence via a GET request.
If you do want to present the GraphiQL interface when the GraphQL endpoint is loaded in a browser then all you need to do is to register a GET method:
```js
const restify = require('restify');
const graphqlHTTP = require('express-graphql');
const app = restify.createServer();
app.get('/graphql', graphqlHTTP({
schema: MyGraphQLSchema,
graphiql: true
}));
app.listen(4000);
```
Having both the POST and the GET methods registered on the same endpoint will not cause any issue and will allow you to receive GraphQL queries through GET request using query params and POST requests using a payload in addition to optionally serve the GraphiQL interface when accessing `/graphql` from the browser; here is the full configuration:
```js
const restify = require('restify');
const graphqlHTTP = require('express-graphql');
const app = restify.createServer();
app.post('/graphql', graphqlHTTP({
schema: MyGraphQLSchema,
graphiql: false
}));
app.get('/graphql', graphqlHTTP({
schema: MyGraphQLSchema,
graphiql: true // set it to false to disable the GraphiQL interface
}));
app.listen(4000);
```
## Options

@@ -69,3 +133,18 @@

In addition to an object defining each option, options can also be provided as
a function (or async function) which returns this options object. This function
is provided the arguments `(request, response, graphQLParams)` and is called
after the request has been parsed.
The `graphQLParams` is provided as the object `{ query, variables, operationName, raw }`.
```js
app.use('/graphql', graphqlHTTP(async (request, response, graphQLParams) => ({
schema: MyGraphQLSchema,
rootValue: await someFunctionToGetRootValue(request)
graphiql: true
})));
```
## HTTP Usage

@@ -72,0 +151,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc