Skip to content

Commit

Permalink
GraphQLError: don't wrap single node in array (#1757)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Mar 1, 2019
1 parent 8013c0d commit fbd9c47
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/error/__tests__/GraphQLError-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('GraphQLError', () => {
});

it('converts node with loc.start === 0 to positions and locations', () => {
const e = new GraphQLError('msg', [operationNode]);
const e = new GraphQLError('msg', operationNode);
expect(e).to.have.property('source', source);
expect(e).to.deep.include({
nodes: [operationNode],
Expand All @@ -115,7 +115,7 @@ describe('GraphQLError', () => {
});

it('serializes to include message and locations', () => {
const e = new GraphQLError('msg', [fieldNode]);
const e = new GraphQLError('msg', fieldNode);
expect(JSON.stringify(e)).to.equal(
'{"message":"msg","locations":[{"line":2,"column":3}]}',
);
Expand Down
12 changes: 6 additions & 6 deletions src/execution/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function getVariableValues(
`"${print(
varDefNode.type,
)}" which cannot be used as an input type.`,
[varDefNode.type],
varDefNode.type,
),
);
} else {
Expand All @@ -82,7 +82,7 @@ export function getVariableValues(
`"${inspect(varType)}" must not be null.`
: `Variable "$${varName}" of required type ` +
`"${inspect(varType)}" was not provided.`,
[varDefNode],
varDefNode,
),
);
} else if (hasValue) {
Expand Down Expand Up @@ -161,21 +161,21 @@ export function getArgumentValues(
throw new GraphQLError(
`Argument "${name}" of non-null type "${inspect(argType)}" ` +
'must not be null.',
[argumentNode.value],
argumentNode.value,
);
} else if (argumentNode && argumentNode.value.kind === Kind.VARIABLE) {
const variableName = argumentNode.value.name.value;
throw new GraphQLError(
`Argument "${name}" of required type "${inspect(argType)}" ` +
`was provided the variable "$${variableName}" ` +
'which was not provided a runtime value.',
[argumentNode.value],
argumentNode.value,
);
} else {
throw new GraphQLError(
`Argument "${name}" of required type "${inspect(argType)}" ` +
'was not provided.',
[node],
node,
);
}
} else if (hasValue) {
Expand All @@ -199,7 +199,7 @@ export function getArgumentValues(
// continue with an invalid argument value.
throw new GraphQLError(
`Argument "${name}" has invalid value ${print(valueNode)}.`,
[argumentNode.value],
argumentNode.value,
);
}
coercedValues[name] = coercedValue;
Expand Down
2 changes: 1 addition & 1 deletion src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class SchemaValidationContext {
message: string,
nodes?: $ReadOnlyArray<?ASTNode> | ?ASTNode,
): void {
const _nodes = (Array.isArray(nodes) ? nodes : [nodes]).filter(Boolean);
const _nodes = Array.isArray(nodes) ? nodes.filter(Boolean) : nodes;
this.addError(new GraphQLError(message, _nodes));
}

Expand Down
4 changes: 2 additions & 2 deletions src/utilities/findDeprecatedUsages.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function findDeprecatedUsages(
new GraphQLError(
`The field ${parentType.name}.${fieldDef.name} is deprecated.` +
(reason ? ' ' + reason : ''),
[node],
node,
),
);
}
Expand All @@ -55,7 +55,7 @@ export function findDeprecatedUsages(
new GraphQLError(
`The enum value ${type.name}.${enumVal.name} is deprecated.` +
(reason ? ' ' + reason : ''),
[node],
node,
),
);
}
Expand Down
14 changes: 8 additions & 6 deletions src/utilities/getOperationRootType.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,32 @@ export function getOperationRootType(
if (!queryType) {
throw new GraphQLError(
'Schema does not define the required query root type.',
[operation],
operation,
);
}
return queryType;
case 'mutation':
const mutationType = schema.getMutationType();
if (!mutationType) {
throw new GraphQLError('Schema is not configured for mutations.', [
throw new GraphQLError(
'Schema is not configured for mutations.',
operation,
]);
);
}
return mutationType;
case 'subscription':
const subscriptionType = schema.getSubscriptionType();
if (!subscriptionType) {
throw new GraphQLError('Schema is not configured for subscriptions.', [
throw new GraphQLError(
'Schema is not configured for subscriptions.',
operation,
]);
);
}
return subscriptionType;
default:
throw new GraphQLError(
'Can only have query, mutation and subscription operations.',
[operation],
operation,
);
}
}
2 changes: 1 addition & 1 deletion src/validation/rules/ExecutableDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function ExecutableDefinitions(
? 'schema'
: definition.name.value,
),
[definition],
definition,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/FieldsOnCorrectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function FieldsOnCorrectType(context: ValidationContext): ASTVisitor {
suggestedTypeNames,
suggestedFieldNames,
),
[node],
node,
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/validation/rules/FragmentsOnCompositeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function FragmentsOnCompositeTypes(
context.reportError(
new GraphQLError(
inlineFragmentOnNonCompositeErrorMessage(print(typeCondition)),
[typeCondition],
typeCondition,
),
);
}
Expand All @@ -62,7 +62,7 @@ export function FragmentsOnCompositeTypes(
node.name.value,
print(node.typeCondition),
),
[node.typeCondition],
node.typeCondition,
),
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/validation/rules/KnownDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ export function KnownDirectives(

if (!locations) {
context.reportError(
new GraphQLError(unknownDirectiveMessage(name), [node]),
new GraphQLError(unknownDirectiveMessage(name), node),
);
return;
}
const candidateLocation = getDirectiveLocationForASTPath(ancestors);
if (candidateLocation && locations.indexOf(candidateLocation) === -1) {
context.reportError(
new GraphQLError(misplacedDirectiveMessage(name, candidateLocation), [
new GraphQLError(
misplacedDirectiveMessage(name, candidateLocation),
node,
]),
),
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/KnownFragmentNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function KnownFragmentNames(context: ValidationContext): ASTVisitor {
const fragment = context.getFragment(fragmentName);
if (!fragment) {
context.reportError(
new GraphQLError(unknownFragmentMessage(fragmentName), [node.name]),
new GraphQLError(unknownFragmentMessage(fragmentName), node.name),
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/LoneAnonymousOperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function LoneAnonymousOperation(
OperationDefinition(node) {
if (!node.name && operationCount > 1) {
context.reportError(
new GraphQLError(anonOperationNotAloneMessage(), [node]),
new GraphQLError(anonOperationNotAloneMessage(), node),
);
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/NoUnusedFragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function NoUnusedFragments(context: ASTValidationContext): ASTVisitor {
const fragName = fragmentDef.name.value;
if (fragmentNameUsed[fragName] !== true) {
context.reportError(
new GraphQLError(unusedFragMessage(fragName), [fragmentDef]),
new GraphQLError(unusedFragMessage(fragName), fragmentDef),
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/validation/rules/NoUnusedVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ export function NoUnusedVariables(context: ValidationContext): ASTVisitor {
const variableName = variableDef.variable.name.value;
if (variableNameUsed[variableName] !== true) {
context.reportError(
new GraphQLError(unusedVariableMessage(variableName, opName), [
new GraphQLError(
unusedVariableMessage(variableName, opName),
variableDef,
]),
),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/validation/rules/PossibleFragmentSpreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function PossibleFragmentSpreads(
inspect(parentType),
inspect(fragType),
),
[node],
node,
),
);
}
Expand All @@ -82,7 +82,7 @@ export function PossibleFragmentSpreads(
inspect(parentType),
inspect(fragType),
),
[node],
node,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/ProvidedRequiredArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function ProvidedRequiredArguments(
argDef.name,
inspect(argDef.type),
),
[fieldNode],
fieldNode,
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/validation/rules/ScalarLeafs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export function ScalarLeafs(context: ValidationContext): ASTVisitor {
context.reportError(
new GraphQLError(
noSubselectionAllowedMessage(node.name.value, inspect(type)),
[selectionSet],
selectionSet,
),
);
}
} else if (!selectionSet) {
context.reportError(
new GraphQLError(
requiredSubselectionMessage(node.name.value, inspect(type)),
[node],
node,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/VariablesAreInputTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function VariablesAreInputTypes(context: ValidationContext): ASTVisitor {
context.reportError(
new GraphQLError(
nonInputTypeOnVarMessage(variableName, print(node.type)),
[node.type],
node.type,
),
);
}
Expand Down

0 comments on commit fbd9c47

Please sign in to comment.