graphql-tools
Advanced tools
Comparing version 0.3.4 to 0.3.6
@@ -7,2 +7,5 @@ 'use strict'; | ||
exports.apolloServer = undefined; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
exports.default = apolloServer; | ||
@@ -22,87 +25,123 @@ | ||
function apolloServer(_ref) // pass through | ||
{ | ||
var schema = _ref.schema; | ||
var // required | ||
resolvers = _ref.resolvers; | ||
var // required if mocks is not false and schema is not GraphQLSchema | ||
connectors = _ref.connectors; | ||
var // required if mocks is not false and schema is not GraphQLSchema | ||
logger = _ref.logger; | ||
var _ref$mocks = _ref.mocks; | ||
var mocks = _ref$mocks === undefined ? false : _ref$mocks; | ||
var _ref$allowUndefinedIn = _ref.allowUndefinedInResolve; | ||
var allowUndefinedInResolve = _ref$allowUndefinedIn === undefined ? false : _ref$allowUndefinedIn; | ||
var formatError = _ref.formatError; | ||
var _ref$graphiql = _ref.graphiql; | ||
var // pass through | ||
graphiql = _ref$graphiql === undefined ? true : _ref$graphiql; | ||
var // pass through | ||
validationRules = _ref.validationRules; | ||
var // pass through | ||
context = _ref.context; | ||
var // pass through | ||
rootValue = _ref.rootValue; | ||
// TODO this implementation could use a bit of refactoring. | ||
// it turned from a simple function into something promise-based, | ||
// which means the structure is now quite awkward. | ||
// TODO: throw an error if more than one arg is passed | ||
// TODO: throw an error if that argument is not an object | ||
if (!schema) { | ||
throw new Error('schema is required'); | ||
function apolloServer(options) { | ||
if (!options) { | ||
throw new Error('GraphQL middleware requires options.'); | ||
} | ||
if (!logger) { | ||
// eslint-disable-next-line no-param-reassign | ||
logger = { log: function log(x) { | ||
return console.log(x); | ||
} }; | ||
if (arguments.length - 1 > 0) { | ||
throw new Error('apolloServer expects exactly one argument, got ' + (arguments.length - 1 + 1)); | ||
} | ||
if (!context) { | ||
// eslint-disable-next-line no-param-reassign | ||
context = {}; | ||
} | ||
var executableSchema = void 0; | ||
if (mocks) { | ||
var myMocks = mocks || {}; | ||
executableSchema = (0, _schemaGenerator.buildSchemaFromTypeDefinitions)(schema); | ||
(0, _mock.addMockFunctionsToSchema)({ | ||
schema: executableSchema, | ||
mocks: myMocks | ||
}); | ||
} else { | ||
// this is just basics, makeExecutableSchema should catch the rest | ||
// TODO: should be able to provide a GraphQLschema and still use resolvers | ||
// and connectors if you want, but at the moment this is not possible. | ||
if (schema instanceof _graphql.GraphQLSchema) { | ||
executableSchema = (0, _schemaGenerator.makeExecutableSchema)({ | ||
schema: schema, | ||
resolvers: {}, | ||
connectors: {}, | ||
logger: logger, | ||
allowUndefinedInResolve: allowUndefinedInResolve | ||
}); | ||
} else { | ||
if (!resolvers) { | ||
throw new Error('resolvers is required option if mocks is not provided'); | ||
// Resolve the Options to get OptionsData. | ||
return function (req, res) { | ||
new Promise(function (resolve) { | ||
resolve(typeof options === 'function' ? options(req) : options); | ||
}).then(function (optionsData) { | ||
// Assert that optionsData is in fact an Object. | ||
if (!optionsData || (typeof optionsData === 'undefined' ? 'undefined' : _typeof(optionsData)) !== 'object') { | ||
throw new Error('GraphQL middleware option function must return an options object.'); | ||
} | ||
if (!connectors) { | ||
throw new Error('connectors is a required option if mocks is not provided'); | ||
// Assert that schema is required. | ||
if (!optionsData.schema) { | ||
throw new Error('GraphQL middleware options must contain a schema.'); | ||
} | ||
executableSchema = (0, _schemaGenerator.makeExecutableSchema)({ | ||
typeDefs: schema, | ||
resolvers: resolvers, | ||
connectors: connectors, | ||
logger: logger, | ||
allowUndefinedInResolve: allowUndefinedInResolve | ||
}); | ||
} | ||
} | ||
var schema = // pass through | ||
optionsData.schema; | ||
var // required | ||
resolvers = optionsData.resolvers; | ||
var // required if mocks is not false and schema is not GraphQLSchema | ||
connectors = optionsData.connectors; | ||
var // required if mocks is not false and schema is not GraphQLSchema | ||
logger = optionsData.logger; | ||
var printErrors = optionsData.printErrors; | ||
var _optionsData$mocks = optionsData.mocks; | ||
var mocks = _optionsData$mocks === undefined ? false : _optionsData$mocks; | ||
var _optionsData$allowUnd = optionsData.allowUndefinedInResolve; | ||
var allowUndefinedInResolve = _optionsData$allowUnd === undefined ? true : _optionsData$allowUnd; | ||
var pretty = optionsData.pretty; | ||
var _optionsData$graphiql = optionsData.graphiql; | ||
var // pass through | ||
graphiql = _optionsData$graphiql === undefined ? false : _optionsData$graphiql; | ||
var // pass through | ||
validationRules = optionsData.validationRules; | ||
var _optionsData$context = optionsData.context; | ||
var // pass through | ||
context = _optionsData$context === undefined ? {} : _optionsData$context; | ||
var // pass through | ||
rootValue = optionsData.rootValue; | ||
return function (req, res, next) { | ||
return (0, _expressGraphql2.default)({ | ||
schema: executableSchema, | ||
context: context, | ||
formatError: formatError, | ||
rootValue: rootValue, | ||
validationRules: validationRules, | ||
graphiql: graphiql | ||
})(req, res, next); | ||
// would collide with formatError from graphql otherwise | ||
var formatErrorFn = optionsData.formatError; | ||
var executableSchema = void 0; | ||
if (mocks) { | ||
// TODO: mocks doesn't yet work with a normal GraphQL schema, but it should! | ||
// have to rewrite these functions | ||
var myMocks = mocks || {}; | ||
executableSchema = (0, _schemaGenerator.buildSchemaFromTypeDefinitions)(schema); | ||
(0, _mock.addMockFunctionsToSchema)({ | ||
schema: executableSchema, | ||
mocks: myMocks | ||
}); | ||
} else { | ||
// this is just basics, makeExecutableSchema should catch the rest | ||
// TODO: should be able to provide a GraphQLschema and still use resolvers | ||
// and connectors if you want, but at the moment this is not possible. | ||
if (schema instanceof _graphql.GraphQLSchema) { | ||
if (logger) { | ||
(0, _schemaGenerator.addErrorLoggingToSchema)(schema, logger); | ||
} | ||
if (printErrors) { | ||
(0, _schemaGenerator.addErrorLoggingToSchema)(schema, { log: function log(e) { | ||
return console.error(e.stack); | ||
} }); | ||
} | ||
if (!allowUndefinedInResolve) { | ||
(0, _schemaGenerator.addCatchUndefinedToSchema)(schema); | ||
} | ||
executableSchema = schema; | ||
} else { | ||
if (!resolvers) { | ||
// TODO: test this error | ||
throw new Error('resolvers is required option if mocks is not provided'); | ||
} | ||
executableSchema = (0, _schemaGenerator.makeExecutableSchema)({ | ||
typeDefs: schema, | ||
resolvers: resolvers, | ||
connectors: connectors, | ||
logger: logger, | ||
allowUndefinedInResolve: allowUndefinedInResolve | ||
}); | ||
if (printErrors) { | ||
(0, _schemaGenerator.addErrorLoggingToSchema)(executableSchema, { log: function log(e) { | ||
return console.error(e.stack); | ||
} }); | ||
} | ||
} | ||
} | ||
// graphQLHTTPOptions | ||
return { | ||
schema: executableSchema, | ||
pretty: pretty, | ||
formatError: formatErrorFn, | ||
validationRules: validationRules, | ||
context: context, | ||
rootValue: rootValue, | ||
graphiql: graphiql | ||
}; | ||
}).then(function (graphqlHTTPOptions) { | ||
return (0, _expressGraphql2.default)(graphqlHTTPOptions)(req, res); | ||
}).catch(function (error) { | ||
// express-graphql catches its own errors, this is just for | ||
// errors in Apollo-server. | ||
// XXX we should probably care about formatErrorFn and pretty. | ||
res.status(error.status || 500); | ||
var result = { errors: [error] }; | ||
result.errors = result.errors.map(_graphql.formatError); | ||
res.set('Content-Type', 'application/json').send(JSON.stringify(result)); | ||
}); | ||
}; | ||
@@ -109,0 +148,0 @@ } |
@@ -10,2 +10,5 @@ 'use strict'; | ||
// TODO: document each function clearly in the code: what arguments it accepts | ||
// and what it outputs. | ||
var _language = require('graphql/language'); | ||
@@ -31,5 +34,4 @@ | ||
// TODO: make this an object | ||
function generateSchema(typeDefinitions, resolveFunctions) { | ||
var logger = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; | ||
var forbidUndefinedInResolve = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3]; | ||
function generateSchema(typeDefinitions, resolveFunctions, logger) { | ||
var allowUndefinedInResolve = arguments.length <= 3 || arguments[3] === undefined ? true : arguments[3]; | ||
@@ -51,3 +53,3 @@ if (!typeDefinitions) { | ||
if (forbidUndefinedInResolve) { | ||
if (!allowUndefinedInResolve) { | ||
addCatchUndefinedToSchema(schema); | ||
@@ -63,2 +65,4 @@ } | ||
// TODO: this function is almost the same as generateSchema. Merge them. | ||
// or maybe don't export generate schema. | ||
function makeExecutableSchema(_ref) { | ||
@@ -68,6 +72,3 @@ var typeDefs = _ref.typeDefs; | ||
var connectors = _ref.connectors; | ||
var _ref$logger = _ref.logger; | ||
var logger = _ref$logger === undefined ? { log: function log(x) { | ||
return console.log(x.stack); | ||
} } : _ref$logger; | ||
var logger = _ref.logger; | ||
var _ref$allowUndefinedIn = _ref.allowUndefinedInResolve; | ||
@@ -82,3 +83,7 @@ var allowUndefinedInResolve = _ref$allowUndefinedIn === undefined ? false : _ref$allowUndefinedIn; | ||
} | ||
attachConnectorsToContext(jsSchema, connectors); | ||
if (connectors) { | ||
// connectors are optional, at least for now. That means you can just import them in the resolve | ||
// function if you want. | ||
attachConnectorsToContext(jsSchema, connectors); | ||
} | ||
return jsSchema; | ||
@@ -113,2 +118,3 @@ } | ||
function buildSchemaFromTypeDefinitions(typeDefinitions) { | ||
// TODO: accept only array here, otherwise interfaces get confusing. | ||
var myDefinitions = typeDefinitions; | ||
@@ -274,3 +280,4 @@ if (typeof myDefinitions !== 'string') { | ||
if (typeof fn === 'undefined') { | ||
return undefined; | ||
// eslint-disable-next-line no-param-reassign | ||
fn = defaultResolveFn; | ||
} | ||
@@ -281,6 +288,10 @@ return function () { | ||
} catch (e) { | ||
// TODO: clone the error properly | ||
var newE = new Error(); | ||
newE.stack = e.stack; | ||
if (hint) { | ||
e.message = 'Error in resolver ' + hint + '\n' + e.message; | ||
newE.originalMessage = e.message; | ||
newE.message = 'Error in resolver ' + hint + '\n' + e.message; | ||
} | ||
logger.log(e); | ||
logger.log(newE); | ||
// we want to pass on the error, just in case. | ||
@@ -309,2 +320,6 @@ throw e; | ||
function decorateToCatchUndefined(fn, hint) { | ||
if (typeof fn === 'undefined') { | ||
// eslint-disable-next-line no-param-reassign | ||
fn = defaultResolveFn; | ||
} | ||
return function () { | ||
@@ -311,0 +326,0 @@ var result = fn.apply(undefined, arguments); |
{ | ||
"name": "graphql-tools", | ||
"version": "0.3.4", | ||
"version": "0.3.6", | ||
"description": "A set of useful GraphQL tools", | ||
@@ -50,2 +50,3 @@ "main": "dist/index.js", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"body-parser": "^1.15.0", | ||
"chai": "^3.5.0", | ||
@@ -57,8 +58,10 @@ "dataloader": "^1.1.0", | ||
"express": "^4.13.4", | ||
"express3": "0.0.0", | ||
"istanbul": "1.0.0-alpha.2", | ||
"mocha": "^2.3.3", | ||
"multer": "^1.0.3", | ||
"nodemon": "^1.9.1", | ||
"request-promise": "^2.0.1", | ||
"supertest": "^1.2.0", | ||
"supertest-as-promised": "^3.1.0" | ||
"supertest": "^1.0.1", | ||
"supertest-as-promised": "^2.0.2" | ||
}, | ||
@@ -65,0 +68,0 @@ "eslintConfig": { |
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
43531
893
21