Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3aa2a73

Browse files
authored
Merge pull request graphql#907 from alexandrebodin/allow-union-type-begining-pipe
Add support for leading vertical bar in union types
2 parents c4f2656 + 7fc9fc0 commit 3aa2a73

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/language/__tests__/schema-kitchen-sink.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ union Feed = Story | Article | Advert
3737

3838
union AnnotatedUnion @onUnion = A | B
3939

40+
union AnnotatedUnionTwo @onUnion = | A | B
41+
4042
scalar CustomScalar
4143

4244
scalar AnnotatedScalar @onScalar

src/language/__tests__/schema-parser-test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,58 @@ type Hello {
457457
expect(printJson(doc)).to.equal(printJson(expected));
458458
});
459459

460+
it('Union with two types and leading vertical bar', () => {
461+
const body = 'union Hello = | Wo | Rld';
462+
const doc = parse(body);
463+
const expected = {
464+
kind: 'Document',
465+
definitions: [
466+
{
467+
kind: 'UnionTypeDefinition',
468+
name: nameNode('Hello', { start: 6, end: 11 }),
469+
directives: [],
470+
types: [
471+
typeNode('Wo', { start: 16, end: 18 }),
472+
typeNode('Rld', { start: 21, end: 24 }),
473+
],
474+
loc: { start: 0, end: 24 },
475+
}
476+
],
477+
loc: { start: 0, end: 24 },
478+
};
479+
expect(printJson(doc)).to.equal(printJson(expected));
480+
});
481+
482+
it('Union with no types and leading vertical bar', () => {
483+
const body = 'union Hello = |';
484+
expect(() => parse(body)).to.throw();
485+
});
486+
487+
it('Union with types and ending vertical bar', () => {
488+
const body = 'union Hello = Wo | Rld |';
489+
expect(() => parse(body)).to.throw();
490+
});
491+
492+
it('Union with types and double vertical bar at the beginning', () => {
493+
const body = 'union Hello = || Wo | Rld';
494+
expect(() => parse(body)).to.throw();
495+
});
496+
497+
it('Union with types and double vertical bar in the middle', () => {
498+
const body = 'union Hello = Wo || Rld';
499+
expect(() => parse(body)).to.throw();
500+
});
501+
502+
it('Union with types and double vertical bar at the end', () => {
503+
const body = 'union Hello = | Wo | Rld ||';
504+
expect(() => parse(body)).to.throw();
505+
});
506+
507+
it('Union with types , leanding and ending vertical bar', () => {
508+
const body = 'union Hello = | Wo | Rld |';
509+
expect(() => parse(body)).to.throw();
510+
});
511+
460512
it('Union with two types', () => {
461513
const body = 'union Hello = Wo | Rld';
462514
const doc = parse(body);

src/language/__tests__/schema-printer-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ union Feed = Story | Article | Advert
8383
8484
union AnnotatedUnion @onUnion = A | B
8585
86+
union AnnotatedUnionTwo @onUnion = A | B
87+
8688
scalar CustomScalar
8789
8890
scalar AnnotatedScalar @onScalar

src/language/parser.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,9 @@ function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinitionNode {
943943
*/
944944
function parseUnionMembers(lexer: Lexer<*>): Array<NamedTypeNode> {
945945
const members = [];
946+
if (peek(lexer, TokenKind.PIPE)) {
947+
skip(lexer, TokenKind.PIPE);
948+
}
946949
do {
947950
members.push(parseNamedType(lexer));
948951
} while (skip(lexer, TokenKind.PIPE));

0 commit comments

Comments
 (0)