graphql-server-express
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -11,2 +11,2 @@ import * as express from 'express'; | ||
export declare function graphqlExpress(options: GraphQLOptions | ExpressGraphQLOptionsFunction): ExpressHandler; | ||
export declare function graphiqlExpress(options: GraphiQL.GraphiQLData): (req: express.Request, res: express.Response, next: any) => void; | ||
export declare function graphiqlExpress(options: GraphiQL.GraphiQLData): (req: express.Request, res: express.Response) => void; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments)).next()); | ||
}); | ||
}; | ||
const graphql = require("graphql"); | ||
const url = require("url"); | ||
@@ -21,101 +12,29 @@ const graphql_server_core_1 = require("graphql-server-core"); | ||
} | ||
return (req, res, next) => __awaiter(this, void 0, void 0, function* () { | ||
let optionsObject; | ||
if (isOptionsFunction(options)) { | ||
try { | ||
optionsObject = yield options(req, res); | ||
} | ||
catch (e) { | ||
res.statusCode = 500; | ||
res.write(`Invalid options provided to ApolloServer: ${e.message}`); | ||
res.end(); | ||
} | ||
} | ||
else { | ||
optionsObject = options; | ||
} | ||
const formatErrorFn = optionsObject.formatError || graphql.formatError; | ||
if (req.method !== 'POST') { | ||
res.setHeader('Allow', 'POST'); | ||
res.statusCode = 405; | ||
res.write('Apollo Server supports only POST requests.'); | ||
return (req, res) => { | ||
graphql_server_core_1.runHttpQuery([req, res], { | ||
method: req.method, | ||
options: options, | ||
query: req.method === 'POST' ? req.body : req.query, | ||
}).then((gqlResponse) => { | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.write(gqlResponse); | ||
res.end(); | ||
return; | ||
} | ||
if (!req.body) { | ||
res.statusCode = 500; | ||
res.write('POST body missing. Did you forget "app.use(bodyParser.json())"?'); | ||
res.end(); | ||
return; | ||
} | ||
let b = req.body; | ||
let isBatch = true; | ||
if (!Array.isArray(b)) { | ||
isBatch = false; | ||
b = [b]; | ||
} | ||
let responses = []; | ||
for (let requestParams of b) { | ||
try { | ||
const query = requestParams.query; | ||
const operationName = requestParams.operationName; | ||
let variables = requestParams.variables; | ||
if (typeof variables === 'string') { | ||
try { | ||
variables = JSON.parse(variables); | ||
} | ||
catch (error) { | ||
res.statusCode = 400; | ||
res.write('Variables are invalid JSON.'); | ||
res.end(); | ||
return; | ||
} | ||
} | ||
let context = optionsObject.context; | ||
if (isBatch) { | ||
context = Object.assign({}, context || {}); | ||
} | ||
let params = { | ||
schema: optionsObject.schema, | ||
query: query, | ||
variables: variables, | ||
context: context, | ||
rootValue: optionsObject.rootValue, | ||
operationName: operationName, | ||
logFunction: optionsObject.logFunction, | ||
validationRules: optionsObject.validationRules, | ||
formatError: formatErrorFn, | ||
formatResponse: optionsObject.formatResponse, | ||
debug: optionsObject.debug, | ||
}; | ||
if (optionsObject.formatParams) { | ||
params = optionsObject.formatParams(params); | ||
} | ||
responses.push(yield graphql_server_core_1.runQuery(params)); | ||
}, (error) => { | ||
if ('HttpQueryError' !== error.name) { | ||
throw error; | ||
} | ||
catch (e) { | ||
responses.push({ errors: [formatErrorFn(e)] }); | ||
if (error.headers) { | ||
Object.keys(error.headers).forEach((header) => { | ||
res.setHeader(header, error.headers[header]); | ||
}); | ||
} | ||
} | ||
res.setHeader('Content-Type', 'application/json'); | ||
if (isBatch) { | ||
res.write(JSON.stringify(responses)); | ||
res.statusCode = error.statusCode; | ||
res.write(error.message); | ||
res.end(); | ||
} | ||
else { | ||
const gqlResponse = responses[0]; | ||
if (gqlResponse.errors && typeof gqlResponse.data === 'undefined') { | ||
res.statusCode = 400; | ||
} | ||
res.write(JSON.stringify(gqlResponse)); | ||
res.end(); | ||
} | ||
}); | ||
}); | ||
}; | ||
} | ||
exports.graphqlExpress = graphqlExpress; | ||
function isOptionsFunction(arg) { | ||
return typeof arg === 'function'; | ||
} | ||
function graphiqlExpress(options) { | ||
return (req, res, next) => { | ||
return (req, res) => { | ||
const q = req.url && url.parse(req.url, true).query || {}; | ||
@@ -122,0 +41,0 @@ const query = q.query || ''; |
{ | ||
"name": "graphql-server-express", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Production-ready Node.js GraphQL server for Express and Connect", | ||
@@ -29,3 +29,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"graphql-server-core": "^0.5.0", | ||
"graphql-server-core": "^0.5.1", | ||
"graphql-server-module-graphiql": "^0.4.4" | ||
@@ -38,5 +38,6 @@ }, | ||
"@types/multer": "0.0.32", | ||
"graphql-server-integration-testsuite": "^0.5.0", | ||
"graphql-server-integration-testsuite": "^0.5.1", | ||
"body-parser": "^1.15.2", | ||
"connect": "^3.4.1", | ||
"connect-query": "^0.2.0", | ||
"express": "^4.14.0", | ||
@@ -43,0 +44,0 @@ "multer": "^1.2.0" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7804
10
82
Updatedgraphql-server-core@^0.5.1