Skip to content

Commit

Permalink
Validation: add support of SDL to KnownTypeNames
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Dec 24, 2018
1 parent 070121c commit 3b9ea61
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 214 deletions.
129 changes: 0 additions & 129 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,132 +798,3 @@ describe('Schema Builder', () => {
buildSchema(sdl, { assumeValidSDL: true });
});
});

describe('Failures', () => {
it('Unknown type referenced', () => {
const sdl = `
schema {
query: Hello
}
type Hello {
bar: Bar
}
`;
expect(() => buildSchema(sdl)).to.throw(
'Type "Bar" not found in document.',
);
});

it('Unknown type in interface list', () => {
const sdl = `
type Query implements Bar {
field: String
}
`;
expect(() => buildSchema(sdl)).to.throw(
'Type "Bar" not found in document.',
);
});

it('Unknown type in union list', () => {
const sdl = `
union TestUnion = Bar
type Query { testUnion: TestUnion }
`;
expect(() => buildSchema(sdl)).to.throw(
'Type "Bar" not found in document.',
);
});

it('Unknown query type', () => {
const sdl = `
schema {
query: Wat
}
type Hello {
str: String
}
`;
expect(() => buildSchema(sdl)).to.throw(
'Specified query type "Wat" not found in document.',
);
});

it('Unknown mutation type', () => {
const sdl = `
schema {
query: Hello
mutation: Wat
}
type Hello {
str: String
}
`;
expect(() => buildSchema(sdl)).to.throw(
'Specified mutation type "Wat" not found in document.',
);
});

it('Unknown subscription type', () => {
const sdl = `
schema {
query: Hello
mutation: Wat
subscription: Awesome
}
type Hello {
str: String
}
type Wat {
str: String
}
`;
expect(() => buildSchema(sdl)).to.throw(
'Specified subscription type "Awesome" not found in document.',
);
});

it('Does not consider directive names', () => {
const sdl = `
schema {
query: Foo
}
directive @Foo on QUERY
`;
expect(() => buildSchema(sdl)).to.throw(
'Specified query type "Foo" not found in document.',
);
});

it('Does not consider operation names', () => {
const sdl = `
schema {
query: Foo
}
query Foo { field }
`;
expect(() => buildSchema(sdl)).to.throw(
'Specified query type "Foo" not found in document.',
);
});

it('Does not consider fragment names', () => {
const sdl = `
schema {
query: Foo
}
fragment Foo on Type { field }
`;
expect(() => buildSchema(sdl)).to.throw(
'Specified query type "Foo" not found in document.',
);
});
});
32 changes: 0 additions & 32 deletions src/utilities/__tests__/extendSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,38 +1107,6 @@ describe('extendSchema', () => {
);
});

it('does not allow referencing an unknown type', () => {
const unknownTypeError =
'Unknown type: "Quix". Ensure that this type exists either in the ' +
'original schema, or is added in a type definition.';

const typeSDL = `
extend type Bar {
quix: Quix
}
`;
expect(() => extendTestSchema(typeSDL)).to.throw(unknownTypeError);

const interfaceSDL = `
extend interface SomeInterface {
quix: Quix
}
`;
expect(() => extendTestSchema(interfaceSDL)).to.throw(unknownTypeError);

const unionSDL = `
extend union SomeUnion = Quix
`;
expect(() => extendTestSchema(unionSDL)).to.throw(unknownTypeError);

const inputSDL = `
extend input SomeInput {
quix: Quix
}
`;
expect(() => extendTestSchema(inputSDL)).to.throw(unknownTypeError);
});

it('does not allow extending an unknown type', () => {
[
'extend scalar UnknownType @foo',
Expand Down
9 changes: 1 addition & 8 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,7 @@ export function buildASTSchema(
function getOperationTypes(schema: SchemaDefinitionNode) {
const opTypes = {};
for (const operationType of schema.operationTypes) {
const typeName = operationType.type.name.value;
const operation = operationType.operation;
if (!nodeMap[typeName]) {
throw new Error(
`Specified ${operation} type "${typeName}" not found in document.`,
);
}
opTypes[operation] = operationType.type;
opTypes[operationType.operation] = operationType.type;
}
return opTypes;
}
Expand Down
10 changes: 2 additions & 8 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,9 @@ export function extendSchema(
typeRef => {
const typeName = typeRef.name.value;
const existingType = schema.getType(typeName);
if (existingType) {
return extendNamedType(existingType);
}

throw new GraphQLError(
`Unknown type: "${typeName}". Ensure that this type exists ` +
'either in the original schema, or is added in a type definition.',
[typeRef],
);
invariant(existingType, `Unknown type: "${typeName}".`);
return extendNamedType(existingType);
},
);

Expand Down
Loading

0 comments on commit 3b9ea61

Please sign in to comment.