diff --git a/src/language/__tests__/schema-kitchen-sink.graphql b/src/language/__tests__/schema-kitchen-sink.graphql index a56c0e4cc6..0686414894 100644 --- a/src/language/__tests__/schema-kitchen-sink.graphql +++ b/src/language/__tests__/schema-kitchen-sink.graphql @@ -37,6 +37,8 @@ union Feed = Story | Article | Advert union AnnotatedUnion @onUnion = A | B +union AnnotatedUnionTwo @onUnion = | A | B + scalar CustomScalar scalar AnnotatedScalar @onScalar diff --git a/src/language/__tests__/schema-parser-test.js b/src/language/__tests__/schema-parser-test.js index 3a15e300e5..0e76282679 100644 --- a/src/language/__tests__/schema-parser-test.js +++ b/src/language/__tests__/schema-parser-test.js @@ -457,6 +457,58 @@ type Hello { expect(printJson(doc)).to.equal(printJson(expected)); }); + it('Union with two types and leading vertical bar', () => { + const body = 'union Hello = | Wo | Rld'; + const doc = parse(body); + const expected = { + kind: 'Document', + definitions: [ + { + kind: 'UnionTypeDefinition', + name: nameNode('Hello', { start: 6, end: 11 }), + directives: [], + types: [ + typeNode('Wo', { start: 16, end: 18 }), + typeNode('Rld', { start: 21, end: 24 }), + ], + loc: { start: 0, end: 24 }, + } + ], + loc: { start: 0, end: 24 }, + }; + expect(printJson(doc)).to.equal(printJson(expected)); + }); + + it('Union with no types and leading vertical bar', () => { + const body = 'union Hello = |'; + expect(() => parse(body)).to.throw(); + }); + + it('Union with types and ending vertical bar', () => { + const body = 'union Hello = Wo | Rld |'; + expect(() => parse(body)).to.throw(); + }); + + it('Union with types and double vertical bar at the beginning', () => { + const body = 'union Hello = || Wo | Rld'; + expect(() => parse(body)).to.throw(); + }); + + it('Union with types and double vertical bar in the middle', () => { + const body = 'union Hello = Wo || Rld'; + expect(() => parse(body)).to.throw(); + }); + + it('Union with types and double vertical bar at the end', () => { + const body = 'union Hello = | Wo | Rld ||'; + expect(() => parse(body)).to.throw(); + }); + + it('Union with types , leanding and ending vertical bar', () => { + const body = 'union Hello = | Wo | Rld |'; + expect(() => parse(body)).to.throw(); + }); + it('Union with two types', () => { const body = 'union Hello = Wo | Rld'; const doc = parse(body); diff --git a/src/language/__tests__/schema-printer-test.js b/src/language/__tests__/schema-printer-test.js index 4fa56275bb..89558f8a9f 100644 --- a/src/language/__tests__/schema-printer-test.js +++ b/src/language/__tests__/schema-printer-test.js @@ -83,6 +83,8 @@ union Feed = Story | Article | Advert union AnnotatedUnion @onUnion = A | B +union AnnotatedUnionTwo @onUnion = A | B + scalar CustomScalar scalar AnnotatedScalar @onScalar diff --git a/src/language/parser.js b/src/language/parser.js index ea4ac0f68c..03bb35097e 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -943,6 +943,9 @@ function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinitionNode { */ function parseUnionMembers(lexer: Lexer<*>): Array { const members = []; + if (peek(lexer, TokenKind.PIPE)) { + skip(lexer, TokenKind.PIPE); + } do { members.push(parseNamedType(lexer)); } while (skip(lexer, TokenKind.PIPE));