Skip to content

[RFC] Type condition optional on inline fragments. #191

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 1 commit into from
Oct 2, 2015
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
6 changes: 5 additions & 1 deletion src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,11 @@ function doesFragmentConditionMatch(
fragment: FragmentDefinition | InlineFragment,
type: GraphQLObjectType
): boolean {
var conditionalType = typeFromAST(exeContext.schema, fragment.typeCondition);
var typeConditionAST = fragment.typeCondition;
if (!typeConditionAST) {
return true;
}
var conditionalType = typeFromAST(exeContext.schema, typeConditionAST);
if (conditionalType === type) {
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/language/__tests__/kitchen-sink.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ query queryName($foo: ComplexType, $site: Site = MOBILE) {
}
}
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/language/__tests__/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ describe('Printer', () => {
}
}
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/language/__tests__/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,34 @@ describe('Visitor', () => {
[ 'leave', 'Field', 0, undefined ],
[ 'leave', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
[ 'leave', 'InlineFragment', 1, undefined ],
[ 'enter', 'InlineFragment', 2, undefined ],
[ 'enter', 'Directive', 0, undefined ],
[ 'enter', 'Name', 'name', 'Directive' ],
[ 'leave', 'Name', 'name', 'Directive' ],
[ 'enter', 'Argument', 0, undefined ],
[ 'enter', 'Name', 'name', 'Argument' ],
[ 'leave', 'Name', 'name', 'Argument' ],
[ 'enter', 'Variable', 'value', 'Argument' ],
[ 'enter', 'Name', 'name', 'Variable' ],
[ 'leave', 'Name', 'name', 'Variable' ],
[ 'leave', 'Variable', 'value', 'Argument' ],
[ 'leave', 'Argument', 0, undefined ],
[ 'leave', 'Directive', 0, undefined ],
[ 'enter', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
[ 'enter', 'Field', 0, undefined ],
[ 'enter', 'Name', 'name', 'Field' ],
[ 'leave', 'Name', 'name', 'Field' ],
[ 'leave', 'Field', 0, undefined ],
[ 'leave', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
[ 'leave', 'InlineFragment', 2, undefined ],
[ 'enter', 'InlineFragment', 3, undefined ],
[ 'enter', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
[ 'enter', 'Field', 0, undefined ],
[ 'enter', 'Name', 'name', 'Field' ],
[ 'leave', 'Name', 'name', 'Field' ],
[ 'leave', 'Field', 0, undefined ],
[ 'leave', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
[ 'leave', 'InlineFragment', 3, undefined ],
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
[ 'leave', 'Field', 0, undefined ],
[ 'leave', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
Expand Down
2 changes: 1 addition & 1 deletion src/language/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export type FragmentSpread = {
export type InlineFragment = {
kind: 'InlineFragment';
loc?: ?Location;
typeCondition: NamedType;
typeCondition?: ?NamedType;
directives?: ?Array<Directive>;
selectionSet: SelectionSet;
}
Expand Down
20 changes: 12 additions & 8 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,25 +377,29 @@ function parseArgument(parser): Argument {
*
* FragmentSpread : ... FragmentName Directives?
*
* InlineFragment : ... on TypeCondition Directives? SelectionSet
* InlineFragment : ... TypeCondition? Directives? SelectionSet
*/
function parseFragment(parser): FragmentSpread | InlineFragment {
var start = parser.token.start;
expect(parser, TokenKind.SPREAD);
if (parser.token.value === 'on') {
advance(parser);
if (peek(parser, TokenKind.NAME) && parser.token.value !== 'on') {
return {
kind: INLINE_FRAGMENT,
typeCondition: parseNamedType(parser),
kind: FRAGMENT_SPREAD,
name: parseFragmentName(parser),
directives: parseDirectives(parser),
selectionSet: parseSelectionSet(parser),
loc: loc(parser, start)
};
}
var typeCondition = null;
if (parser.token.value === 'on') {
advance(parser);
typeCondition = parseNamedType(parser);
}
return {
kind: FRAGMENT_SPREAD,
name: parseFragmentName(parser),
kind: INLINE_FRAGMENT,
typeCondition,
directives: parseDirectives(parser),
selectionSet: parseSelectionSet(parser),
loc: loc(parser, start)
};
}
Expand Down
9 changes: 6 additions & 3 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ var printDocASTReducer = {
'...' + name + wrap(' ', join(directives, ' ')),

InlineFragment: ({ typeCondition, directives, selectionSet }) =>
`... on ${typeCondition} ` +
wrap('', join(directives, ' '), ' ') +
selectionSet,
join([
'...',
wrap('on ', typeCondition),
join(directives, ' '),
selectionSet
], ' '),

FragmentDefinition: ({ name, typeCondition, directives, selectionSet }) =>
`fragment ${name} on ${typeCondition} ` +
Expand Down
5 changes: 4 additions & 1 deletion src/utilities/TypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export class TypeInfo {
break;
case Kind.INLINE_FRAGMENT:
case Kind.FRAGMENT_DEFINITION:
type = typeFromAST(schema, node.typeCondition);
var typeConditionAST = node.typeCondition;
type = typeConditionAST ?
typeFromAST(schema, typeConditionAST) :
this.getType();
this._typeStack.push(type);
break;
case Kind.VARIABLE_DEFINITION:
Expand Down
3 changes: 3 additions & 0 deletions src/validation/__tests__/FieldsOnCorrectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ describe('Validate: Fields on correct type', () => {
... on Dog {
name
}
... {
name
}
}
`);
});
Expand Down
3 changes: 3 additions & 0 deletions src/validation/__tests__/KnownFragmentNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ describe('Validate: Known fragment names', () => {
... on Human {
...HumanFields2
}
... {
name
}
}
}
fragment HumanFields1 on Human {
Expand Down
2 changes: 1 addition & 1 deletion src/validation/__tests__/KnownTypeNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Validate: Known type names', () => {
expectPassesRule(KnownTypeNames, `
query Foo($var: String, $required: [String!]!) {
user(id: 4) {
pets { ... on Pet { name }, ...PetFields }
pets { ... on Pet { name }, ...PetFields, ... { name } }
}
}
fragment PetFields on Pet {
Expand Down