Skip to content

validation-test: Improve typings #2710

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
Jul 15, 2020
Merged
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
95 changes: 52 additions & 43 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import type {
GraphQLNamedType,
GraphQLInputType,
GraphQLOutputType,
GraphQLFieldConfig,
GraphQLArgumentConfig,
GraphQLInputFieldConfig,
GraphQLEnumValueConfigMap,
} from '../definition';
import { GraphQLSchema } from '../schema';
import { GraphQLString } from '../scalars';
Expand Down Expand Up @@ -103,13 +107,12 @@ const notInputTypes: Array<GraphQLOutputType> = [
...withModifiers(SomeInterfaceType),
];

function schemaWithFieldType(type) {
function schemaWithFieldType(type: GraphQLOutputType): GraphQLSchema {
return new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: { f: { type } },
}),
types: [type],
});
}

Expand Down Expand Up @@ -516,18 +519,20 @@ describe('Type System: Fields args must be properly named', () => {
});

it('rejects field arg with invalid names', () => {
const QueryType = new GraphQLObjectType({
name: 'SomeObject',
fields: {
badField: {
type: GraphQLString,
args: {
'bad-name-with-dashes': { type: GraphQLString },
const schema = schemaWithFieldType(
new GraphQLObjectType({
name: 'SomeObject',
fields: {
badField: {
type: GraphQLString,
args: {
'bad-name-with-dashes': { type: GraphQLString },
},
},
},
},
});
const schema = new GraphQLSchema({ query: QueryType });
}),
);

expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand Down Expand Up @@ -928,7 +933,7 @@ describe('Type System: Enum types must be well defined', () => {
});

it('rejects an Enum type with incorrectly named values', () => {
function schemaWithEnum(values) {
function schemaWithEnum(values: GraphQLEnumValueConfigMap): GraphQLSchema {
return schemaWithFieldType(
new GraphQLEnumType({
name: 'SomeEnum',
Expand Down Expand Up @@ -979,11 +984,13 @@ describe('Type System: Enum types must be well defined', () => {
});

describe('Type System: Object fields must have output types', () => {
function schemaWithObjectFieldOfType(fieldType: GraphQLOutputType) {
function schemaWithObjectField(
fieldConfig: GraphQLFieldConfig<mixed, mixed>,
): GraphQLSchema {
const BadObjectType = new GraphQLObjectType({
name: 'BadObject',
fields: {
badField: { type: fieldType },
badField: fieldConfig,
},
});

Expand All @@ -1001,14 +1008,14 @@ describe('Type System: Object fields must have output types', () => {
for (const type of outputTypes) {
const typeName = inspect(type);
it(`accepts an output type as an Object field type: ${typeName}`, () => {
const schema = schemaWithObjectFieldOfType(type);
const schema = schemaWithObjectField({ type });
expect(validateSchema(schema)).to.deep.equal([]);
});
}

it('rejects an empty Object field type', () => {
// $FlowExpectedError
const schema = schemaWithObjectFieldOfType(undefined);
const schema = schemaWithObjectField({ type: undefined });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand All @@ -1021,7 +1028,7 @@ describe('Type System: Object fields must have output types', () => {
const typeStr = inspect(type);
it(`rejects a non-output type as an Object field type: ${typeStr}`, () => {
// $FlowExpectedError
const schema = schemaWithObjectFieldOfType(type);
const schema = schemaWithObjectField({ type });
expect(validateSchema(schema)).to.deep.equal([
{
message: `The type of BadObject.badField must be Output Type but got: ${typeStr}.`,
Expand All @@ -1032,7 +1039,7 @@ describe('Type System: Object fields must have output types', () => {

it('rejects a non-type value as an Object field type', () => {
// $FlowExpectedError
const schema = schemaWithObjectFieldOfType(Number);
const schema = schemaWithObjectField({ type: Number });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand Down Expand Up @@ -1292,20 +1299,20 @@ describe('Type System: Interface extensions should be valid', () => {
});

describe('Type System: Interface fields must have output types', () => {
function schemaWithInterfaceFieldOfType(fieldType: GraphQLOutputType) {
function schemaWithInterfaceField(
fieldConfig: GraphQLFieldConfig<mixed, mixed>,
): GraphQLSchema {
const fields = { badField: fieldConfig };

const BadInterfaceType = new GraphQLInterfaceType({
name: 'BadInterface',
fields: {
badField: { type: fieldType },
},
fields,
});

const BadImplementingType = new GraphQLObjectType({
name: 'BadImplementing',
interfaces: [BadInterfaceType],
fields: {
badField: { type: fieldType },
},
fields,
});

return new GraphQLSchema({
Expand All @@ -1322,14 +1329,14 @@ describe('Type System: Interface fields must have output types', () => {
for (const type of outputTypes) {
const typeName = inspect(type);
it(`accepts an output type as an Interface field type: ${typeName}`, () => {
const schema = schemaWithInterfaceFieldOfType(type);
const schema = schemaWithInterfaceField({ type });
expect(validateSchema(schema)).to.deep.equal([]);
});
}

it('rejects an empty Interface field type', () => {
// $FlowExpectedError
const schema = schemaWithInterfaceFieldOfType(undefined);
const schema = schemaWithInterfaceField({ type: undefined });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand All @@ -1346,7 +1353,7 @@ describe('Type System: Interface fields must have output types', () => {
const typeStr = inspect(type);
it(`rejects a non-output type as an Interface field type: ${typeStr}`, () => {
// $FlowExpectedError
const schema = schemaWithInterfaceFieldOfType(type);
const schema = schemaWithInterfaceField({ type });
expect(validateSchema(schema)).to.deep.equal([
{
message: `The type of BadImplementing.badField must be Output Type but got: ${typeStr}.`,
Expand All @@ -1360,7 +1367,7 @@ describe('Type System: Interface fields must have output types', () => {

it('rejects a non-type value as an Interface field type', () => {
// $FlowExpectedError
const schema = schemaWithInterfaceFieldOfType(Number);
const schema = schemaWithInterfaceField({ type: Number });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand Down Expand Up @@ -1423,14 +1430,14 @@ describe('Type System: Interface fields must have output types', () => {
});

describe('Type System: Arguments must have input types', () => {
function schemaWithArgOfType(argType: GraphQLInputType) {
function schemaWithArg(argConfig: GraphQLArgumentConfig): GraphQLSchema {
const BadObjectType = new GraphQLObjectType({
name: 'BadObject',
fields: {
badField: {
type: GraphQLString,
args: {
badArg: { type: argType },
badArg: argConfig,
},
},
},
Expand All @@ -1447,7 +1454,7 @@ describe('Type System: Arguments must have input types', () => {
new GraphQLDirective({
name: 'BadDirective',
args: {
badArg: { type: argType },
badArg: argConfig,
},
locations: ['QUERY'],
}),
Expand All @@ -1458,14 +1465,14 @@ describe('Type System: Arguments must have input types', () => {
for (const type of inputTypes) {
const typeName = inspect(type);
it(`accepts an input type as a field arg type: ${typeName}`, () => {
const schema = schemaWithArgOfType(type);
const schema = schemaWithArg({ type });
expect(validateSchema(schema)).to.deep.equal([]);
});
}

it('rejects an empty field arg type', () => {
// $FlowExpectedError
const schema = schemaWithArgOfType(undefined);
const schema = schemaWithArg({ type: undefined });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand All @@ -1482,7 +1489,7 @@ describe('Type System: Arguments must have input types', () => {
const typeStr = inspect(type);
it(`rejects a non-input type as a field arg type: ${typeStr}`, () => {
// $FlowExpectedError
const schema = schemaWithArgOfType(type);
const schema = schemaWithArg({ type });
expect(validateSchema(schema)).to.deep.equal([
{
message: `The type of @BadDirective(badArg:) must be Input Type but got: ${typeStr}.`,
Expand All @@ -1496,7 +1503,7 @@ describe('Type System: Arguments must have input types', () => {

it('rejects a non-type value as a field arg type', () => {
// $FlowExpectedError
const schema = schemaWithArgOfType(Number);
const schema = schemaWithArg({ type: Number });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand Down Expand Up @@ -1533,11 +1540,13 @@ describe('Type System: Arguments must have input types', () => {
});

describe('Type System: Input Object fields must have input types', () => {
function schemaWithInputFieldOfType(inputFieldType: GraphQLInputType) {
function schemaWithInputField(
inputFieldConfig: GraphQLInputFieldConfig,
): GraphQLSchema {
const BadInputObjectType = new GraphQLInputObjectType({
name: 'BadInputObject',
fields: {
badField: { type: inputFieldType },
badField: inputFieldConfig,
},
});

Expand All @@ -1559,14 +1568,14 @@ describe('Type System: Input Object fields must have input types', () => {
for (const type of inputTypes) {
const typeName = inspect(type);
it(`accepts an input type as an input field type: ${typeName}`, () => {
const schema = schemaWithInputFieldOfType(type);
const schema = schemaWithInputField({ type });
expect(validateSchema(schema)).to.deep.equal([]);
});
}

it('rejects an empty input field type', () => {
// $FlowExpectedError
const schema = schemaWithInputFieldOfType(undefined);
const schema = schemaWithInputField({ type: undefined });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand All @@ -1579,7 +1588,7 @@ describe('Type System: Input Object fields must have input types', () => {
const typeStr = inspect(type);
it(`rejects a non-input type as an input field type: ${typeStr}`, () => {
// $FlowExpectedError
const schema = schemaWithInputFieldOfType(type);
const schema = schemaWithInputField({ type });
expect(validateSchema(schema)).to.deep.equal([
{
message: `The type of BadInputObject.badField must be Input Type but got: ${typeStr}.`,
Expand All @@ -1590,7 +1599,7 @@ describe('Type System: Input Object fields must have input types', () => {

it('rejects a non-type value as an input field type', () => {
// $FlowExpectedError
const schema = schemaWithInputFieldOfType(Number);
const schema = schemaWithInputField({ type: Number });
expect(validateSchema(schema)).to.deep.equal([
{
message:
Expand Down