Skip to content
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

[RFC] Add directives to field definitions #180

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/language/__tests__/schema-kitchen-sink.graphql
Expand Up @@ -11,6 +11,7 @@ type Foo implements Bar {
three(argument: InputType, other: String): Int
four(argument: String = "string"): String
five(argument: [String] = ["string", "string"]): String
@meta(value: 6)
six(argument: InputType = {key: "value"}): Type
}

Expand Down
55 changes: 55 additions & 0 deletions src/language/__tests__/schema-parser.js
Expand Up @@ -47,11 +47,16 @@ function fieldNode(name, type, loc) {
}

function fieldNodeWithArgs(name, type, args, loc) {
return fieldNodeWithDirectives(name, type, args, [], loc);
}

function fieldNodeWithDirectives(name, type, args, directives, loc) {
return {
kind: 'FieldDefinition',
name,
arguments: args,
type,
directives,
loc,
};
}
Expand Down Expand Up @@ -445,6 +450,56 @@ type Hello {
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple field with a directive', () => {
var body = `
type Hello {
@relatedField(name: "hellos")
world: World
}`;
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'Document',
definitions: [
{
kind: 'ObjectTypeDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
fieldNodeWithDirectives(
nameNode('world', loc(48, 53)),
typeNode('World', loc(55, 60)),
[],
[
{
kind: 'Directive',
name: nameNode('relatedField', loc(17, 29)),
arguments: [
{
kind: 'Argument',
name: nameNode('name', loc(30, 34)),
value: {
kind: 'StringValue',
value: 'hellos',
loc: loc(36, 44),
},
loc: loc(30, 44)
},
],
loc: loc(16, 45),
},
],
loc(16, 60)
)
],
loc: loc(1, 62),
}
],
loc: loc(1, 62),
};
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple union', () => {
var body = `union Hello = World`;
var doc = parse(body);
Expand Down
1 change: 1 addition & 0 deletions src/language/ast.js
Expand Up @@ -276,6 +276,7 @@ export type FieldDefinition = {
name: Name;
arguments: Array<InputValueDefinition>;
type: Type;
directives?: ?Array<Directive>;
}

export type InputValueDefinition = {
Expand Down
2 changes: 2 additions & 0 deletions src/language/parser.js
Expand Up @@ -719,6 +719,7 @@ function parseImplementsInterfaces(parser): Array<NamedType> {
*/
function parseFieldDefinition(parser): FieldDefinition {
var start = parser.token.start;
var directives = parseDirectives(parser);
var name = parseName(parser);
var args = parseArgumentDefs(parser);
expect(parser, TokenKind.COLON);
Expand All @@ -728,6 +729,7 @@ function parseFieldDefinition(parser): FieldDefinition {
name,
arguments: args,
type,
directives,
loc: loc(parser, start),
};
}
Expand Down