Skip to content
Open
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
13 changes: 13 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,19 @@ describe('Schema Builder', () => {
expect(() => buildSchema(sdl)).to.throw('Unknown directive "@unknown".');
});

it('Rejects SDL with invalid directive argument values', () => {
const sdl = `
type Query {
foo: String @test(arg: UNQUOTED_STRING)
}

directive @test(arg: String!) on FIELD_DEFINITION
`;
expect(() => buildSchema(sdl)).to.throw(
'String cannot represent a non string value: UNQUOTED_STRING',
);
});

it('Allows to disable SDL validation', () => {
const sdl = `
type Query {
Expand Down
123 changes: 122 additions & 1 deletion src/validation/__tests__/ValuesOfCorrectTypeRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import { GraphQLObjectType, GraphQLScalarType } from '../../type/definition';
import { GraphQLString } from '../../type/scalars';
import { GraphQLSchema } from '../../type/schema';

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

import {
ValuesOfCorrectTypeOnDirectivesRule,
ValuesOfCorrectTypeRule,
} from '../rules/ValuesOfCorrectTypeRule';
import { validate } from '../validate';

import {
expectSDLValidationErrors,
expectValidationErrors,
expectValidationErrorsWithSchema,
} from './harness';
Expand All @@ -39,6 +45,18 @@ function expectValidWithSchema(schema: GraphQLSchema, queryStr: string) {
expectErrorsWithSchema(schema, queryStr).toDeepEqual([]);
}

function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) {
return expectSDLValidationErrors(
schema,
ValuesOfCorrectTypeOnDirectivesRule,
sdlStr,
);
}

function expectValidSDL(sdlStr: string, schema?: GraphQLSchema) {
expectSDLErrors(sdlStr, schema).toDeepEqual([]);
}

describe('Validate: Values of correct type', () => {
describe('Valid values', () => {
it('Good int value', () => {
Expand Down Expand Up @@ -1143,6 +1161,109 @@ describe('Validate: Values of correct type', () => {
},
]);
});

describe('within SDL', () => {
it('with directive arguments of valid types', () => {
expectValidSDL(`
enum DirectiveEnum {
VALUE
}

input DirectiveInput {
enabled: Boolean!
}

type Query {
field: String @test(
str: "value"
int: 1
enum: VALUE
input: { enabled: true }
)
}

directive @test(
str: String
int: Int
enum: DirectiveEnum
input: DirectiveInput
) on FIELD_DEFINITION
`);
});

it('with directive argument of invalid type', () => {
expectSDLErrors(`
type Query {
baz: Boolean @foo(bar: FOOBAR)
}

directive @foo(bar: String!) on FIELD_DEFINITION
`).toDeepEqual([
{
message: 'String cannot represent a non string value: FOOBAR',
locations: [{ line: 3, column: 36 }],
},
]);
});

it('with directive input object argument of invalid type', () => {
expectSDLErrors(`
input DirectiveInput {
count: Int!
}

type Query {
field: String @test(input: { count: "one" })
}

directive @test(input: DirectiveInput) on FIELD_DEFINITION
`).toDeepEqual([
{
message: 'Int cannot represent non-integer value: "one"',
locations: [{ line: 7, column: 49 }],
},
]);
});

it('with directive argument types extended inside SDL', () => {
const schema = buildSchema(`
enum DirectiveEnum {
OLD
}

input DirectiveInput {
old: String
}

type Query {
field: String
}

directive @test(
enum: DirectiveEnum
input: DirectiveInput
) on OBJECT
`);

expectValidSDL(
`
extend enum DirectiveEnum {
VALUE
}

extend input DirectiveInput {
enabled: Boolean!
}

extend type Query @test(
enum: VALUE
input: { enabled: true }
)
`,
schema,
);
});
});
});

describe('Variable default values', () => {
Expand Down
Loading