Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include fields for GraphQLError #273

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/error/GraphQLError.js
Expand Up @@ -20,6 +20,7 @@ export class GraphQLError extends Error {
source: Source;
positions: Array<number>;
locations: any;
fields: string;
originalError: ?Error;

constructor(
Expand Down Expand Up @@ -70,5 +71,23 @@ export class GraphQLError extends Error {
}
}
}: any));

Object.defineProperty(this, 'fields', ({
get() {
if (nodes && nodes.length) {
var node = nodes[0];
var fields = [];
while (node) {
if (node.alias && node.alias.value) {
fields.unshift(node.alias.value);
} else if (node.name && node.name.value) {
fields.unshift(node.name.value);
}
node = node.parentField;
}
return fields.join('.');
}
}
}: any));
}
}
13 changes: 9 additions & 4 deletions src/execution/execute.js
Expand Up @@ -340,7 +340,8 @@ function collectFields(
runtimeType: GraphQLObjectType,
selectionSet: SelectionSet,
fields: {[key: string]: Array<Field>},
visitedFragmentNames: {[key: string]: boolean}
visitedFragmentNames: {[key: string]: boolean},
parentField: ?Field
): {[key: string]: Array<Field>} {
for (var i = 0; i < selectionSet.selections.length; i++) {
var selection = selectionSet.selections[i];
Expand All @@ -353,6 +354,7 @@ function collectFields(
if (!fields[name]) {
fields[name] = [];
}
selection.parentField = parentField;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to ensure that we're not mutating the provided AST, as it may be used elsewhere.

fields[name].push(selection);
break;
case Kind.INLINE_FRAGMENT:
Expand All @@ -365,7 +367,8 @@ function collectFields(
runtimeType,
selection.selectionSet,
fields,
visitedFragmentNames
visitedFragmentNames,
parentField
);
break;
case Kind.FRAGMENT_SPREAD:
Expand All @@ -386,7 +389,8 @@ function collectFields(
runtimeType,
fragment.selectionSet,
fields,
visitedFragmentNames
visitedFragmentNames,
parentField
);
break;
}
Expand Down Expand Up @@ -749,7 +753,8 @@ function completeValue(
runtimeType,
selectionSet,
subFieldASTs,
visitedFragmentNames
visitedFragmentNames,
fieldASTs[i]
);
}
}
Expand Down