Skip to content

Commit

Permalink
add implements to SDL for unions
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Apr 5, 2022
1 parent 5f247e0 commit b49653f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/language/__tests__/schema-parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,24 @@ describe('Schema Parser', () => {
});
});

it('Union extension without types', () => {
const doc = parse('extend union HelloOrGoodbye implements Greeting');
expectJSON(doc).toDeepEqual({
kind: 'Document',
definitions: [
{
kind: 'UnionTypeExtension',
name: nameNode('HelloOrGoodbye', { start: 13, end: 27 }),
interfaces: [typeNode('Greeting', { start: 39, end: 47 })],
directives: [],
types: [],
loc: { start: 0, end: 47 },
},
],
loc: { start: 0, end: 47 },
});
});

it('Object extension without fields followed by extension', () => {
const doc = parse(`
extend type Hello implements Greeting
Expand Down Expand Up @@ -880,6 +898,7 @@ describe('Schema Parser', () => {
kind: 'UnionTypeDefinition',
name: nameNode('Hello', { start: 6, end: 11 }),
description: undefined,
interfaces: [],
directives: [],
types: [typeNode('World', { start: 14, end: 19 })],
loc: { start: 0, end: 19 },
Expand All @@ -899,6 +918,7 @@ describe('Schema Parser', () => {
kind: 'UnionTypeDefinition',
name: nameNode('Hello', { start: 6, end: 11 }),
description: undefined,
interfaces: [],
directives: [],
types: [
typeNode('Wo', { start: 14, end: 16 }),
Expand All @@ -921,6 +941,7 @@ describe('Schema Parser', () => {
kind: 'UnionTypeDefinition',
name: nameNode('Hello', { start: 6, end: 11 }),
description: undefined,
interfaces: [],
directives: [],
types: [
typeNode('Wo', { start: 16, end: 18 }),
Expand Down
12 changes: 10 additions & 2 deletions src/language/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,13 @@ export const QueryDocumentKeys: {
'directives',
'fields',
],
UnionTypeDefinition: ['description', 'name', 'directives', 'types'],
UnionTypeDefinition: [
'description',
'name',
'interfaces',
'directives',
'types',
],
EnumTypeDefinition: ['description', 'name', 'directives', 'values'],
EnumValueDefinition: ['description', 'name', 'directives'],
InputObjectTypeDefinition: ['description', 'name', 'directives', 'fields'],
Expand All @@ -274,7 +280,7 @@ export const QueryDocumentKeys: {
ScalarTypeExtension: ['name', 'directives'],
ObjectTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
InterfaceTypeExtension: ['name', 'interfaces', 'directives', 'fields'],
UnionTypeExtension: ['name', 'directives', 'types'],
UnionTypeExtension: ['name', 'interfaces', 'directives', 'types'],
EnumTypeExtension: ['name', 'directives', 'values'],
InputObjectTypeExtension: ['name', 'directives', 'fields'],
};
Expand Down Expand Up @@ -624,6 +630,7 @@ export interface UnionTypeDefinitionNode {
readonly loc?: Location;
readonly description?: StringValueNode;
readonly name: NameNode;
readonly interfaces?: ReadonlyArray<NamedTypeNode>;
readonly directives?: ReadonlyArray<ConstDirectiveNode>;
readonly types?: ReadonlyArray<NamedTypeNode>;
}
Expand Down Expand Up @@ -716,6 +723,7 @@ export interface UnionTypeExtensionNode {
readonly kind: Kind.UNION_TYPE_EXTENSION;
readonly loc?: Location;
readonly name: NameNode;
readonly interfaces?: ReadonlyArray<NamedTypeNode>;
readonly directives?: ReadonlyArray<ConstDirectiveNode>;
readonly types?: ReadonlyArray<NamedTypeNode>;
}
Expand Down
10 changes: 9 additions & 1 deletion src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,12 +970,14 @@ export class Parser {
const description = this.parseDescription();
this.expectKeyword('union');
const name = this.parseName();
const interfaces = this.parseImplementsInterfaces();
const directives = this.parseConstDirectives();
const types = this.parseUnionMemberTypes();
return this.node<UnionTypeDefinitionNode>(start, {
kind: Kind.UNION_TYPE_DEFINITION,
description,
name,
interfaces,
directives,
types,
});
Expand Down Expand Up @@ -1249,14 +1251,20 @@ export class Parser {
this.expectKeyword('extend');
this.expectKeyword('union');
const name = this.parseName();
const interfaces = this.parseImplementsInterfaces();
const directives = this.parseConstDirectives();
const types = this.parseUnionMemberTypes();
if (directives.length === 0 && types.length === 0) {
if (
interfaces.length === 0 &&
directives.length === 0 &&
types.length === 0
) {
throw this.unexpected();
}
return this.node<UnionTypeExtensionNode>(start, {
kind: Kind.UNION_TYPE_EXTENSION,
name,
interfaces,
directives,
types,
});
Expand Down

0 comments on commit b49653f

Please sign in to comment.