Skip to content

Add support for leading vertical bar in union types #907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/language/__tests__/schema-kitchen-sink.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ union Feed = Story | Article | Advert

union AnnotatedUnion @onUnion = A | B

union AnnotatedUnionTwo @onUnion = | A | B

scalar CustomScalar

scalar AnnotatedScalar @onScalar
Expand Down
52 changes: 52 additions & 0 deletions src/language/__tests__/schema-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});

Copy link
Contributor

@robzhu robzhu Jun 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add test cases for | Wo | Rld |, union Hello = Wo | Rld |, Wo || Rld and union Hello = |?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

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);
Expand Down
2 changes: 2 additions & 0 deletions src/language/__tests__/schema-printer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ union Feed = Story | Article | Advert

union AnnotatedUnion @onUnion = A | B

union AnnotatedUnionTwo @onUnion = A | B
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to have | A | B?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope the printer will only print without it or with it I think it's better to leave it out and add it with prettier for ex


scalar CustomScalar

scalar AnnotatedScalar @onScalar
Expand Down
3 changes: 3 additions & 0 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,9 @@ function parseUnionTypeDefinition(lexer: Lexer<*>): UnionTypeDefinitionNode {
*/
function parseUnionMembers(lexer: Lexer<*>): Array<NamedTypeNode> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every method in the parser has parse language atop it. It would be nice to include this here as well

const members = [];
if (peek(lexer, TokenKind.PIPE)) {
skip(lexer, TokenKind.PIPE);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapping peek is unnecessary. skip is effectively a call to peek.

do {
members.push(parseNamedType(lexer));
} while (skip(lexer, TokenKind.PIPE));
Expand Down