Skip to content

Validation: Remove internal functions for error messages #2159

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

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/validation/__tests__/ExecutableDefinitions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import { describe, it } from 'mocha';

import {
ExecutableDefinitions,
nonExecutableDefinitionMessage,
} from '../rules/ExecutableDefinitions';
import { ExecutableDefinitions } from '../rules/ExecutableDefinitions';

import { expectValidationErrors } from './harness';

Expand All @@ -17,13 +14,6 @@ function expectValid(queryStr) {
expectErrors(queryStr).to.deep.equal([]);
}

function nonExecutableDefinition(defName, line, column) {
return {
message: nonExecutableDefinitionMessage(defName),
locations: [{ line, column }],
};
}

describe('Validate: Executable definitions', () => {
it('with only operation', () => {
expectValid(`
Expand Down Expand Up @@ -66,8 +56,14 @@ describe('Validate: Executable definitions', () => {
color: String
}
`).to.deep.equal([
nonExecutableDefinition('Cow', 8, 7),
nonExecutableDefinition('Dog', 12, 7),
{
message: 'The Cow definition is not executable.',
locations: [{ line: 8, column: 7 }],
},
{
message: 'The Dog definition is not executable.',
locations: [{ line: 12, column: 7 }],
},
]);
});

Expand All @@ -83,9 +79,18 @@ describe('Validate: Executable definitions', () => {

extend schema @directive
`).to.deep.equal([
nonExecutableDefinition('schema', 2, 7),
nonExecutableDefinition('Query', 6, 7),
nonExecutableDefinition('schema', 10, 7),
{
message: 'The schema definition is not executable.',
locations: [{ line: 2, column: 7 }],
},
{
message: 'The Query definition is not executable.',
locations: [{ line: 6, column: 7 }],
},
{
message: 'The schema definition is not executable.',
locations: [{ line: 10, column: 7 }],
},
]);
});
});
198 changes: 146 additions & 52 deletions src/validation/__tests__/FieldsOnCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import {
FieldsOnCorrectType,
undefinedFieldMessage,
} from '../rules/FieldsOnCorrectType';
import { parse } from '../../language/parser';

import { buildSchema } from '../../utilities/buildASTSchema';

import { validate } from '../validate';
import { FieldsOnCorrectType } from '../rules/FieldsOnCorrectType';

import { expectValidationErrors } from './harness';

Expand All @@ -18,25 +20,6 @@ function expectValid(queryStr) {
expectErrors(queryStr).to.deep.equal([]);
}

function undefinedField(
field,
type,
suggestedTypes,
suggestedFields,
line,
column,
) {
return {
message: undefinedFieldMessage(
field,
type,
suggestedTypes,
suggestedFields,
),
locations: [{ line, column }],
};
}

describe('Validate: Fields on correct type', () => {
it('Object field selection', () => {
expectValid(`
Expand Down Expand Up @@ -99,8 +82,14 @@ describe('Validate: Fields on correct type', () => {
}
}
`).to.deep.equal([
undefinedField('unknown_pet_field', 'Pet', [], [], 3, 9),
undefinedField('unknown_cat_field', 'Cat', [], [], 5, 13),
{
message: 'Cannot query field "unknown_pet_field" on type "Pet".',
locations: [{ line: 3, column: 9 }],
},
{
message: 'Cannot query field "unknown_cat_field" on type "Cat".',
locations: [{ line: 5, column: 13 }],
},
]);
});

Expand All @@ -110,7 +99,11 @@ describe('Validate: Fields on correct type', () => {
meowVolume
}
`).to.deep.equal([
undefinedField('meowVolume', 'Dog', [], ['barkVolume'], 3, 9),
{
message:
'Cannot query field "meowVolume" on type "Dog". Did you mean "barkVolume"?',
locations: [{ line: 3, column: 9 }],
},
]);
});

Expand All @@ -121,7 +114,12 @@ describe('Validate: Fields on correct type', () => {
deeper_unknown_field
}
}
`).to.deep.equal([undefinedField('unknown_field', 'Dog', [], [], 3, 9)]);
`).to.deep.equal([
{
message: 'Cannot query field "unknown_field" on type "Dog".',
locations: [{ line: 3, column: 9 }],
},
]);
});

it('Sub-field not defined', () => {
Expand All @@ -131,7 +129,12 @@ describe('Validate: Fields on correct type', () => {
unknown_field
}
}
`).to.deep.equal([undefinedField('unknown_field', 'Pet', [], [], 4, 11)]);
`).to.deep.equal([
{
message: 'Cannot query field "unknown_field" on type "Pet".',
locations: [{ line: 4, column: 11 }],
},
]);
});

it('Field not defined on inline fragment', () => {
Expand All @@ -142,7 +145,11 @@ describe('Validate: Fields on correct type', () => {
}
}
`).to.deep.equal([
undefinedField('meowVolume', 'Dog', [], ['barkVolume'], 4, 11),
{
message:
'Cannot query field "meowVolume" on type "Dog". Did you mean "barkVolume"?',
locations: [{ line: 4, column: 11 }],
},
]);
});

Expand All @@ -152,7 +159,11 @@ describe('Validate: Fields on correct type', () => {
volume : mooVolume
}
`).to.deep.equal([
undefinedField('mooVolume', 'Dog', [], ['barkVolume'], 3, 9),
{
message:
'Cannot query field "mooVolume" on type "Dog". Did you mean "barkVolume"?',
locations: [{ line: 3, column: 9 }],
},
]);
});

Expand All @@ -162,7 +173,11 @@ describe('Validate: Fields on correct type', () => {
barkVolume : kawVolume
}
`).to.deep.equal([
undefinedField('kawVolume', 'Dog', [], ['barkVolume'], 3, 9),
{
message:
'Cannot query field "kawVolume" on type "Dog". Did you mean "barkVolume"?',
locations: [{ line: 3, column: 9 }],
},
]);
});

Expand All @@ -171,7 +186,12 @@ describe('Validate: Fields on correct type', () => {
fragment notDefinedOnInterface on Pet {
tailLength
}
`).to.deep.equal([undefinedField('tailLength', 'Pet', [], [], 3, 9)]);
`).to.deep.equal([
{
message: 'Cannot query field "tailLength" on type "Pet".',
locations: [{ line: 3, column: 9 }],
},
]);
});

it('Defined on implementors but not on interface', () => {
Expand All @@ -180,7 +200,11 @@ describe('Validate: Fields on correct type', () => {
nickname
}
`).to.deep.equal([
undefinedField('nickname', 'Pet', ['Dog', 'Cat'], ['name'], 3, 9),
{
message:
'Cannot query field "nickname" on type "Pet". Did you mean to use an inline fragment on "Dog" or "Cat"?',
locations: [{ line: 3, column: 9 }],
},
]);
});

Expand All @@ -197,7 +221,12 @@ describe('Validate: Fields on correct type', () => {
fragment directFieldSelectionOnUnion on CatOrDog {
directField
}
`).to.deep.equal([undefinedField('directField', 'CatOrDog', [], [], 3, 9)]);
`).to.deep.equal([
{
message: 'Cannot query field "directField" on type "CatOrDog".',
locations: [{ line: 3, column: 9 }],
},
]);
});

it('Defined on implementors queried on union', () => {
Expand All @@ -206,14 +235,11 @@ describe('Validate: Fields on correct type', () => {
name
}
`).to.deep.equal([
undefinedField(
'name',
'CatOrDog',
['Being', 'Pet', 'Canine', 'Dog', 'Cat'],
[],
3,
9,
),
{
message:
'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Being", "Pet", "Canine", "Dog", or "Cat"?',
locations: [{ line: 3, column: 9 }],
},
]);
});

Expand All @@ -231,42 +257,110 @@ describe('Validate: Fields on correct type', () => {
});

describe('Fields on correct type error message', () => {
function expectErrorMessage(schema, queryStr) {
const errors = validate(schema, parse(queryStr), [FieldsOnCorrectType]);
expect(errors.length).to.equal(1);
return expect(errors[0].message);
}

it('Works with no suggestions', () => {
expect(undefinedFieldMessage('f', 'T', [], [])).to.equal(
const schema = buildSchema(`
type T {
fieldWithVeryLongNameThatWillNeverBeSuggested: String
}
type Query { t: T }
`);

expectErrorMessage(schema, '{ t { f } }').to.equal(
'Cannot query field "f" on type "T".',
);
});

it('Works with no small numbers of type suggestions', () => {
expect(undefinedFieldMessage('f', 'T', ['A', 'B'], [])).to.equal(
const schema = buildSchema(`
union T = A | B
type Query { t: T }

type A { f: String }
type B { f: String }
`);

expectErrorMessage(schema, '{ t { f } }').to.equal(
'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A" or "B"?',
);
});

it('Works with no small numbers of field suggestions', () => {
expect(undefinedFieldMessage('f', 'T', [], ['z', 'y'])).to.equal(
const schema = buildSchema(`
type T {
z: String
y: String
}
type Query { t: T }
`);

expectErrorMessage(schema, '{ t { f } }').to.equal(
'Cannot query field "f" on type "T". Did you mean "z" or "y"?',
);
});

it('Only shows one set of suggestions at a time, preferring types', () => {
expect(undefinedFieldMessage('f', 'T', ['A', 'B'], ['z', 'y'])).to.equal(
const schema = buildSchema(`
interface T {
z: String
y: String
}
type Query { t: T }

type A implements T {
f: String
z: String
y: String
}
type B implements T {
f: String
z: String
y: String
}
`);

expectErrorMessage(schema, '{ t { f } }').to.equal(
'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A" or "B"?',
);
});

it('Limits lots of type suggestions', () => {
expect(
undefinedFieldMessage('f', 'T', ['A', 'B', 'C', 'D', 'E', 'F'], []),
).to.equal(
const schema = buildSchema(`
union T = A | B | C | D | E | F
type Query { t: T }

type A { f: String }
type B { f: String }
type C { f: String }
type D { f: String }
type E { f: String }
type F { f: String }
`);

expectErrorMessage(schema, '{ t { f } }').to.equal(
'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A", "B", "C", "D", or "E"?',
);
});

it('Limits lots of field suggestions', () => {
expect(
undefinedFieldMessage('f', 'T', [], ['z', 'y', 'x', 'w', 'v', 'u']),
).to.equal(
const schema = buildSchema(`
type T {
z: String
y: String
x: String
w: String
v: String
u: String
}
type Query { t: T }
`);

expectErrorMessage(schema, '{ t { f } }').to.equal(
'Cannot query field "f" on type "T". Did you mean "z", "y", "x", "w", or "v"?',
);
});
Expand Down
Loading