From e0d1fc3c92739c42363fc91a0d833e07ad352e17 Mon Sep 17 00:00:00 2001 From: Alexandre BODIN Date: Tue, 13 Jun 2017 23:48:30 +0200 Subject: [PATCH 1/3] Add support for beginning pipe un union types --- .../__tests__/schema-kitchen-sink.graphql | 2 ++ src/language/__tests__/schema-parser-test.js | 22 +++++++++++++++++++ src/language/__tests__/schema-printer-test.js | 2 ++ src/language/parser.js | 3 +++ 4 files changed, 29 insertions(+) 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..7b3ffd1344 100644 --- a/src/language/__tests__/schema-parser-test.js +++ b/src/language/__tests__/schema-parser-test.js @@ -457,6 +457,28 @@ type Hello { expect(printJson(doc)).to.equal(printJson(expected)); }); + it('Union with two types and begining pipe', () => { + 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 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)); From 4cb3ff008906c0a08833c0dc7e54cfbb65a4b459 Mon Sep 17 00:00:00 2001 From: Alexandre BODIN Date: Wed, 14 Jun 2017 00:30:32 +0200 Subject: [PATCH 2/3] Add use cases with ending vertical bar or empty list of types throwing --- src/language/__tests__/schema-parser-test.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/language/__tests__/schema-parser-test.js b/src/language/__tests__/schema-parser-test.js index 7b3ffd1344..9b97b5dd1e 100644 --- a/src/language/__tests__/schema-parser-test.js +++ b/src/language/__tests__/schema-parser-test.js @@ -457,7 +457,7 @@ type Hello { expect(printJson(doc)).to.equal(printJson(expected)); }); - it('Union with two types and begining pipe', () => { + it('Union with two types and leading vertical bar', () => { const body = 'union Hello = | Wo | Rld'; const doc = parse(body); const expected = { @@ -479,6 +479,21 @@ type Hello { 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 , 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); From 7fc9fc0ba1d8f253a6c5876d71e61ad29aed91d6 Mon Sep 17 00:00:00 2001 From: Alexandre BODIN Date: Wed, 14 Jun 2017 00:37:41 +0200 Subject: [PATCH 3/3] Add doucle bar testing --- src/language/__tests__/schema-parser-test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/language/__tests__/schema-parser-test.js b/src/language/__tests__/schema-parser-test.js index 9b97b5dd1e..0e76282679 100644 --- a/src/language/__tests__/schema-parser-test.js +++ b/src/language/__tests__/schema-parser-test.js @@ -489,6 +489,21 @@ type Hello { 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();