Skip to content

Commit

Permalink
Add coordinate field to schema element definitions
Browse files Browse the repository at this point in the history
* Defines a `GraphQLSchemaElement` base class which defines a `.coordinate` property and `toString`/`toJSON` methods.
* Adds base class to types, fields, arguments, input fields, enum values, and directives.
* Uses this in validation error printing string templates.
  • Loading branch information
leebyron committed Jun 3, 2021
1 parent 98e7541 commit b8ee9a2
Show file tree
Hide file tree
Showing 26 changed files with 473 additions and 536 deletions.
8 changes: 4 additions & 4 deletions src/execution/__tests__/nonnull-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ describe('Execute: handles non-nullable types', () => {
errors: [
{
message:
'Argument "cannotBeNull" of required type "String!" was not provided.',
'Argument Query.withNonNullArg(cannotBeNull:) of required type String! was not provided.',
locations: [{ line: 3, column: 13 }],
path: ['withNonNullArg'],
},
Expand All @@ -643,7 +643,7 @@ describe('Execute: handles non-nullable types', () => {
errors: [
{
message:
'Argument "cannotBeNull" of non-null type "String!" must not be null.',
'Argument Query.withNonNullArg(cannotBeNull:) of non-null type String! must not be null.',
locations: [{ line: 3, column: 42 }],
path: ['withNonNullArg'],
},
Expand Down Expand Up @@ -673,7 +673,7 @@ describe('Execute: handles non-nullable types', () => {
errors: [
{
message:
'Argument "cannotBeNull" of required type "String!" was provided the variable "$testVar" which was not provided a runtime value.',
'Argument Query.withNonNullArg(cannotBeNull:) of required type String! was provided the variable "$testVar" which was not provided a runtime value.',
locations: [{ line: 3, column: 42 }],
path: ['withNonNullArg'],
},
Expand Down Expand Up @@ -701,7 +701,7 @@ describe('Execute: handles non-nullable types', () => {
errors: [
{
message:
'Argument "cannotBeNull" of non-null type "String!" must not be null.',
'Argument Query.withNonNullArg(cannotBeNull:) of non-null type String! must not be null.',
locations: [{ line: 3, column: 43 }],
path: ['withNonNullArg'],
},
Expand Down
21 changes: 11 additions & 10 deletions src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Argument "input" has invalid value ["foo", "bar", "baz"].',
'Argument TestType.fieldWithObjectInput(input:) of type TestInputObject has invalid value ["foo", "bar", "baz"].',
path: ['fieldWithObjectInput'],
locations: [{ line: 3, column: 41 }],
},
Expand Down Expand Up @@ -617,7 +617,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$value" of required type "String!" was not provided.',
'Variable "$value" of required type String! was not provided.',
locations: [{ line: 2, column: 16 }],
},
],
Expand All @@ -636,7 +636,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$value" of non-null type "String!" must not be null.',
'Variable "$value" of non-null type String! must not be null.',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down Expand Up @@ -682,7 +682,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Argument "input" of required type "String!" was not provided.',
'Argument TestType.fieldWithNonNullableStringInput(input:) of required type String! was not provided.',
locations: [{ line: 1, column: 3 }],
path: ['fieldWithNonNullableStringInput'],
},
Expand Down Expand Up @@ -730,7 +730,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Argument "input" of required type "String!" was provided the variable "$foo" which was not provided a runtime value.',
'Argument TestType.fieldWithNonNullableStringInput(input:) of required type String! was provided the variable "$foo" which was not provided a runtime value.',
locations: [{ line: 3, column: 50 }],
path: ['fieldWithNonNullableStringInput'],
},
Expand Down Expand Up @@ -785,7 +785,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" of non-null type "[String]!" must not be null.',
'Variable "$input" of non-null type [String]! must not be null.',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down Expand Up @@ -867,7 +867,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" of non-null type "[String!]!" must not be null.',
'Variable "$input" of non-null type [String!]! must not be null.',
locations: [{ line: 2, column: 16 }],
},
],
Expand Down Expand Up @@ -916,7 +916,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" expected value of type "TestType!" which cannot be used as an input type.',
'Variable "$input" expected value of type TestType! which cannot be used as an input type.',
locations: [{ line: 2, column: 24 }],
},
],
Expand All @@ -935,7 +935,7 @@ describe('Execute: Handles inputs', () => {
errors: [
{
message:
'Variable "$input" expected value of type "UnknownType!" which cannot be used as an input type.',
'Variable "$input" expected value of type UnknownType! which cannot be used as an input type.',
locations: [{ line: 2, column: 24 }],
},
],
Expand Down Expand Up @@ -981,7 +981,8 @@ describe('Execute: Handles inputs', () => {
},
errors: [
{
message: 'Argument "input" has invalid value WRONG_TYPE.',
message:
'Argument TestType.fieldWithDefaultArgumentValue(input:) of type String has invalid value WRONG_TYPE.',
locations: [{ line: 3, column: 48 }],
path: ['fieldWithDefaultArgumentValue'],
},
Expand Down
20 changes: 9 additions & 11 deletions src/execution/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function coerceVariableValues(
const varTypeStr = print(varDefNode.type);
onError(
new GraphQLError(
`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`,
`Variable "$${varName}" expected value of type ${varTypeStr} which cannot be used as an input type.`,
varDefNode.type,
),
);
Expand All @@ -98,10 +98,9 @@ function coerceVariableValues(
if (varDefNode.defaultValue) {
coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
} else if (isNonNullType(varType)) {
const varTypeStr = inspect(varType);
onError(
new GraphQLError(
`Variable "$${varName}" of required type "${varTypeStr}" was not provided.`,
`Variable "$${varName}" of required type ${varType} was not provided.`,
varDefNode,
),
);
Expand All @@ -111,10 +110,9 @@ function coerceVariableValues(

const value = inputs[varName];
if (value === null && isNonNullType(varType)) {
const varTypeStr = inspect(varType);
onError(
new GraphQLError(
`Variable "$${varName}" of non-null type "${varTypeStr}" must not be null.`,
`Variable "$${varName}" of non-null type ${varType} must not be null.`,
varDefNode,
),
);
Expand Down Expand Up @@ -178,8 +176,7 @@ export function getArgumentValues(
coercedValues[name] = argDef.defaultValue;
} else if (isNonNullType(argType)) {
throw new GraphQLError(
`Argument "${name}" of required type "${inspect(argType)}" ` +
'was not provided.',
`Argument ${argDef} of required type ${argType} was not provided.`,
node,
);
}
Expand All @@ -199,7 +196,7 @@ export function getArgumentValues(
coercedValues[name] = argDef.defaultValue;
} else if (isNonNullType(argType)) {
throw new GraphQLError(
`Argument "${name}" of required type "${inspect(argType)}" ` +
`Argument ${argDef} of required type ${argType} ` +
`was provided the variable "$${variableName}" which was not provided a runtime value.`,
valueNode,
);
Expand All @@ -211,8 +208,7 @@ export function getArgumentValues(

if (isNull && isNonNullType(argType)) {
throw new GraphQLError(
`Argument "${name}" of non-null type "${inspect(argType)}" ` +
'must not be null.',
`Argument ${argDef} of non-null type ${argType} must not be null.`,
valueNode,
);
}
Expand All @@ -223,7 +219,9 @@ export function getArgumentValues(
// execution. This is a runtime check to ensure execution does not
// continue with an invalid argument value.
throw new GraphQLError(
`Argument "${name}" has invalid value ${print(valueNode)}.`,
`Argument ${argDef} of type ${argType} has invalid value ${print(
valueNode,
)}.`,
valueNode,
);
}
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ export { graphql, graphqlSync } from './graphql';
/** Create and operate on GraphQL type definitions and schema. */
export {
/** Definitions */
GraphQLSchema,
GraphQLDirective,
GraphQLSchemaElement,
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLField,
GraphQLArgument,
GraphQLEnumValue,
GraphQLInputField,
GraphQLList,
GraphQLNonNull,
/** Standard GraphQL Scalars */
Expand Down Expand Up @@ -144,23 +147,19 @@ export type {
GraphQLSchemaExtensions,
GraphQLDirectiveConfig,
GraphQLDirectiveExtensions,
GraphQLArgument,
GraphQLArgumentConfig,
GraphQLArgumentExtensions,
GraphQLEnumTypeConfig,
GraphQLEnumTypeExtensions,
GraphQLEnumValue,
GraphQLEnumValueConfig,
GraphQLEnumValueConfigMap,
GraphQLEnumValueExtensions,
GraphQLField,
GraphQLFieldConfig,
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFieldExtensions,
GraphQLFieldMap,
GraphQLFieldResolver,
GraphQLInputField,
GraphQLInputFieldConfig,
GraphQLInputFieldConfigMap,
GraphQLInputFieldExtensions,
Expand Down
16 changes: 12 additions & 4 deletions src/type/__tests__/definition-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ describe('Type System: Objects', () => {
},
};
const testObject1 = new GraphQLObjectType({
name: 'Test1',
name: 'Test',
fields: outputFields,
});
const testObject2 = new GraphQLObjectType({
name: 'Test2',
name: 'Test',
fields: outputFields,
});

Expand All @@ -191,11 +191,11 @@ describe('Type System: Objects', () => {
field2: { type: ScalarType },
};
const testInputObject1 = new GraphQLInputObjectType({
name: 'Test1',
name: 'Test',
fields: inputFields,
});
const testInputObject2 = new GraphQLInputObjectType({
name: 'Test2',
name: 'Test',
fields: inputFields,
});

Expand Down Expand Up @@ -243,6 +243,7 @@ describe('Type System: Objects', () => {
});
expect(objType.getFields()).to.deep.equal({
f: {
coordinate: 'SomeObject.f',
name: 'f',
description: undefined,
type: ScalarType,
Expand Down Expand Up @@ -270,11 +271,13 @@ describe('Type System: Objects', () => {
});
expect(objType.getFields()).to.deep.equal({
f: {
coordinate: 'SomeObject.f',
name: 'f',
description: undefined,
type: ScalarType,
args: [
{
coordinate: 'SomeObject.f(arg:)',
name: 'arg',
description: undefined,
type: ScalarType,
Expand Down Expand Up @@ -624,6 +627,7 @@ describe('Type System: Enums', () => {

expect(EnumTypeWithNullishValue.getValues()).to.deep.equal([
{
coordinate: 'EnumWithNullishValue.NULL',
name: 'NULL',
description: undefined,
value: null,
Expand All @@ -632,6 +636,7 @@ describe('Type System: Enums', () => {
astNode: undefined,
},
{
coordinate: 'EnumWithNullishValue.NAN',
name: 'NAN',
description: undefined,
value: NaN,
Expand All @@ -640,6 +645,7 @@ describe('Type System: Enums', () => {
astNode: undefined,
},
{
coordinate: 'EnumWithNullishValue.NO_CUSTOM_VALUE',
name: 'NO_CUSTOM_VALUE',
description: undefined,
value: 'NO_CUSTOM_VALUE',
Expand Down Expand Up @@ -730,6 +736,7 @@ describe('Type System: Input Objects', () => {
});
expect(inputObjType.getFields()).to.deep.equal({
f: {
coordinate: 'SomeInputObject.f',
name: 'f',
description: undefined,
type: ScalarType,
Expand All @@ -750,6 +757,7 @@ describe('Type System: Input Objects', () => {
});
expect(inputObjType.getFields()).to.deep.equal({
f: {
coordinate: 'SomeInputObject.f',
name: 'f',
description: undefined,
type: ScalarType,
Expand Down
2 changes: 2 additions & 0 deletions src/type/__tests__/directive-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('Type System: Directive', () => {
name: 'Foo',
args: [
{
coordinate: '@Foo(foo:)',
name: 'foo',
description: undefined,
type: GraphQLString,
Expand All @@ -42,6 +43,7 @@ describe('Type System: Directive', () => {
astNode: undefined,
},
{
coordinate: '@Foo(bar:)',
name: 'bar',
description: undefined,
type: GraphQLInt,
Expand Down
2 changes: 2 additions & 0 deletions src/type/__tests__/enumType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ describe('Type System: Enum Values', () => {
const values = ComplexEnum.getValues();
expect(values).to.have.deep.ordered.members([
{
coordinate: 'Complex.ONE',
name: 'ONE',
description: undefined,
value: Complex1,
Expand All @@ -350,6 +351,7 @@ describe('Type System: Enum Values', () => {
astNode: undefined,
},
{
coordinate: 'Complex.TWO',
name: 'TWO',
description: undefined,
value: Complex2,
Expand Down
2 changes: 1 addition & 1 deletion src/type/__tests__/introspection-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ describe('Introspection', () => {
errors: [
{
message:
'Field "__type" argument "name" of type "String!" is required, but it was not provided.',
'Argument <meta>.__type(name:) of type "String!" is required, but it was not provided.',
locations: [{ line: 3, column: 9 }],
},
],
Expand Down
Loading

0 comments on commit b8ee9a2

Please sign in to comment.