Socket
Socket
Sign inDemoInstall

graphql

Package Overview
Dependencies
Maintainers
1
Versions
259
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

65

lib/error/index.js

@@ -13,2 +13,6 @@ /* @flow */

var _inherits = require('babel-runtime/helpers/inherits')['default'];
var _get = require('babel-runtime/helpers/get')['default'];
var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];

@@ -24,2 +28,3 @@

exports.locatedError = locatedError;
exports.formatError = formatError;

@@ -33,33 +38,45 @@

var GraphQLError = function GraphQLError(message,
// A flow bug keeps us from declaring nodes as an array of Node
nodes, stack) {
_classCallCheck(this, GraphQLError);
var GraphQLError = (function (_Error) {
function GraphQLError(message,
// A flow bug keeps us from declaring nodes as an array of Node
nodes, stack) {
_classCallCheck(this, GraphQLError);
this.message = message;
this.stack = stack || message;
if (nodes) {
this.nodes = nodes;
var positions = nodes.map(function (node) {
return node.loc && node.loc.start;
});
if (positions.some(function (p) {
return !!p;
})) {
this.positions = positions;
var loc = nodes[0].loc;
var source = loc && loc.source;
if (source) {
this.locations = positions.map(function (pos) {
return (0, _language.getLocation)(source, pos);
});
this.source = source;
_get(Object.getPrototypeOf(GraphQLError.prototype), 'constructor', this).call(this, message);
this.message = message;
this.stack = stack || message;
if (nodes) {
this.nodes = nodes;
var positions = nodes.map(function (node) {
return node.loc && node.loc.start;
});
if (positions.some(function (p) {
return !!p;
})) {
this.positions = positions;
var loc = nodes[0].loc;
var source = loc && loc.source;
if (source) {
this.locations = positions.map(function (pos) {
return (0, _language.getLocation)(source, pos);
});
this.source = source;
}
}
}
}
};
_inherits(GraphQLError, _Error);
return GraphQLError;
})(Error);
exports.GraphQLError = GraphQLError;
GraphQLError.prototype = Error.prototype;
function locatedError(error, nodes) {
if (error instanceof GraphQLError) {
return error;
}
return new GraphQLError(error && error.message, nodes, error ? error.stack : null);
}

@@ -66,0 +83,0 @@ function formatError(error) {

@@ -41,3 +41,3 @@ /* @flow */

/**
* The result of exeution. `data` is the result of executing the
* The result of execution. `data` is the result of executing the
* query, `errors` is null if no errors occurred, and is a

@@ -331,7 +331,11 @@ * non-empty array if an error occurred.

/**
* A wrapper function for resolving the field, that catches the error
* and adds it to the context's global if the error is not rethrowable.
* Resolves the field on the given source object. In particular, this
* figures out the value that the field returns by calling its resolve function,
* then calls completeValue to complete promises, coerce scalars, or execute
* the sub-selection-set for objects.
*/
function resolveField(exeContext, parentType, source, fieldASTs) {
var fieldDef = getFieldDef(exeContext.schema, parentType, fieldASTs[0]);
var fieldAST = fieldASTs[0];
var fieldDef = getFieldDef(exeContext.schema, parentType, fieldAST);
if (!fieldDef) {

@@ -341,33 +345,2 @@ return;

// If the field type is non-nullable, then it is resolved without any
// protection from errors.
if (fieldDef.type instanceof _typeDefinition.GraphQLNonNull) {
return resolveFieldOrError(exeContext, parentType, source, fieldASTs, fieldDef);
}
// Otherwise, error protection is applied, logging the error and resolving
// a null value for this field if one is encountered.
try {
var result = resolveFieldOrError(exeContext, parentType, source, fieldASTs, fieldDef);
if (isThenable(result)) {
return result.then(undefined, function (error) {
exeContext.errors.push(error);
return _Promise.resolve(null);
});
}
return result;
} catch (error) {
exeContext.errors.push(error);
return null;
}
}
/**
* Resolves the field on the given source object. In particular, this
* figures out the object that the field returns using the resolve function,
* then calls completeField to corece scalars or execute the sub
* selection set for objects.
*/
function resolveFieldOrError(exeContext, parentType, source, fieldASTs, fieldDef) {
var fieldAST = fieldASTs[0];
var fieldType = fieldDef.type;

@@ -381,2 +354,6 @@ var resolveFn = fieldDef.resolve || defaultResolveFn;

// If an error occurs while calling the field `resolve` function, ensure that
// it is wrapped as a GraphQLError with locations. Log this error and return
// null if allowed, otherwise throw the error so the parent field can handle
// it.
try {

@@ -387,14 +364,37 @@ var result = resolveFn(source, args, exeContext.root,

} catch (error) {
throw new _error.GraphQLError(error.message, [fieldAST], error.stack);
var reportedError = new _error.GraphQLError(error.message, fieldASTs, error.stack);
if (fieldType instanceof _typeDefinition.GraphQLNonNull) {
throw reportedError;
}
exeContext.errors.push(reportedError);
return null;
}
if (isThenable(result)) {
return result.then(function (resolvedResult) {
return completeField(exeContext, fieldType, fieldASTs, resolvedResult);
}, function (error) {
return _Promise.reject(new _error.GraphQLError(error.message, [fieldAST], error.stack));
});
return completeValueCatchingError(exeContext, fieldType, fieldASTs, result);
}
function completeValueCatchingError(exeContext, fieldType, fieldASTs, result) {
// If the field type is non-nullable, then it is resolved without any
// protection from errors.
if (fieldType instanceof _typeDefinition.GraphQLNonNull) {
return completeValue(exeContext, fieldType, fieldASTs, result);
}
return completeField(exeContext, fieldType, fieldASTs, result);
// Otherwise, error protection is applied, logging the error and resolving
// a null value for this field if one is encountered.
try {
var completed = completeValue(exeContext, fieldType, fieldASTs, result);
if (isThenable(completed)) {
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
return completed.then(undefined, function (error) {
exeContext.errors.push(error);
return _Promise.resolve(null);
});
}
return completed;
} catch (error) {
exeContext.errors.push(error);
return null;
}
}

@@ -419,7 +419,17 @@

*/
function completeField(exeContext, fieldType, fieldASTs, result) {
function completeValue(exeContext, fieldType, fieldASTs, result) {
// If result is a Promise, resolve it, if the Promise is rejected, construct
// a GraphQLError with proper locations.
if (isThenable(result)) {
return result.then(function (resolved) {
return completeValue(exeContext, fieldType, fieldASTs, resolved);
}, function (error) {
return _Promise.reject((0, _error.locatedError)(error, fieldASTs));
});
}
// If field type is NonNull, complete for inner type, and throw field error
// if result is null.
if (fieldType instanceof _typeDefinition.GraphQLNonNull) {
var completed = completeField(exeContext, fieldType.ofType, fieldASTs, result);
var completed = completeValue(exeContext, fieldType.ofType, fieldASTs, result);
if (completed === null) {

@@ -438,7 +448,17 @@ throw new _error.GraphQLError('Cannot return null for non-nullable type.', fieldASTs);

if (fieldType instanceof _typeDefinition.GraphQLList) {
(0, _utilsInvariant2['default'])(Array.isArray(result), 'User Error: expected iterable, but did not find one.');
// This is specified as a simple map, however we're optimizing the path
// where the list contains no Promises by avoiding creating another Promise.
var itemType = fieldType.ofType;
(0, _utilsInvariant2['default'])(Array.isArray(result), 'User Error: expected iterable, but did not find one.');
return result.map(function (item) {
return completeField(exeContext, itemType, fieldASTs, item);
var containsPromise = false;
var completedResults = result.map(function (item) {
var completedItem = completeValueCatchingError(exeContext, itemType, fieldASTs, item);
if (!containsPromise && isThenable(completedItem)) {
containsPromise = true;
}
return completedItem;
});
return containsPromise ? _Promise.all(completedResults) : completedResults;
}

@@ -497,3 +517,3 @@

* It has special casing for the two introspection fields, __schema
* and __typename. __typename is special becuase it can always be
* and __typename. __typename is special because it can always be
* queried as a field, even in situations where no other fields

@@ -500,0 +520,0 @@ * are allowed, like on a Union. __schema could get automatically

@@ -34,3 +34,3 @@ /* @flow /

* it is called, it returns the next token in the Source. Assuming the
* source lexes, the final Token omitted by the lexer will be of kind
* source lexes, the final Token emitted by the lexer will be of kind
* EOF, after which the lexer will repeatedly return EOF tokens whenever

@@ -53,3 +53,3 @@ * called.

/**
* An enum describing the different kinds of tokens that the lexer omits.
* An enum describing the different kinds of tokens that the lexer emits.
*/

@@ -56,0 +56,0 @@ var TokenKind = {

@@ -14,3 +14,3 @@ /* @flow */

* but is mostly useful for clients who store GraphQL documents in
* souce files; for example, if the GraphQL input is in a file Foo.graphql,
* source files; for example, if the GraphQL input is in a file Foo.graphql,
* it might be useful for name to be "Foo.graphql".

@@ -17,0 +17,0 @@ */

@@ -58,3 +58,3 @@ /**

* visit() will walk through an AST using a depth first traversal, calling
* the visitor's enter function at each node in the traveral, and calling the
* the visitor's enter function at each node in the traversal, and calling the
* leave function after visiting that node and all of it's child nodes.

@@ -61,0 +61,0 @@ *

@@ -0,1 +1,10 @@

/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

@@ -2,0 +11,0 @@

{
"name": "graphql",
"version": "0.1.1",
"version": "0.1.2",
"description": "A Query Language and Runtime which can target any service.",

@@ -5,0 +5,0 @@ "contributors": [

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