Skip to content

Commit 1b6824b

Browse files
committed
RFC: Schema Language Directives (#376)
This implements adding directives to the experimental schema language by extending the *locations* a directive can be used. Notice that this provides no semantic meaning to these directives - they are purely a mechanism for annotating an AST - however future directives which contain semantic meaning may be introduced in the future (the first will be `@deprecated`).
1 parent 71b6a4a commit 1b6824b

File tree

13 files changed

+401
-51
lines changed

13 files changed

+401
-51
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,52 @@ type Foo implements Bar {
1919
six(argument: InputType = {key: "value"}): Type
2020
}
2121

22+
type AnnotatedObject @onObject(arg: "value") {
23+
annotatedField(arg: Type = "default" @onArg): Type @onField
24+
}
25+
2226
interface Bar {
2327
one: Type
2428
four(argument: String = "string"): String
2529
}
2630

31+
interface AnnotatedInterface @onInterface {
32+
annotatedField(arg: Type @onArg): Type @onField
33+
}
34+
2735
union Feed = Story | Article | Advert
2836

37+
union AnnotatedUnion @onUnion = A | B
38+
2939
scalar CustomScalar
3040

41+
scalar AnnotatedScalar @onScalar
42+
3143
enum Site {
3244
DESKTOP
3345
MOBILE
3446
}
3547

48+
enum AnnotatedEnum @onEnum {
49+
ANNOTATED_VALUE @onEnumValue
50+
OTHER_VALUE
51+
}
52+
3653
input InputType {
3754
key: String!
3855
answer: Int = 42
3956
}
4057

58+
input AnnotatedInput @onInputObjectType {
59+
annotatedField: Type @onField
60+
}
61+
4162
extend type Foo {
4263
seven(argument: [String]): Type
4364
}
4465

66+
extend type Foo @onType {}
67+
4568
type NoFields {}
4669

4770
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function fieldNodeWithArgs(name, type, args, loc) {
5252
name,
5353
arguments: args,
5454
type,
55+
directives: [],
5556
loc,
5657
};
5758
}
@@ -60,6 +61,7 @@ function enumValueNode(name, loc) {
6061
return {
6162
kind: 'EnumValueDefinition',
6263
name: nameNode(name, loc),
64+
directives: [],
6365
loc,
6466
};
6567
}
@@ -70,6 +72,7 @@ function inputValueNode(name, type, defaultValue, loc) {
7072
name,
7173
type,
7274
defaultValue,
75+
directives: [],
7376
loc,
7477
};
7578
}
@@ -89,6 +92,7 @@ type Hello {
8992
kind: 'ObjectTypeDefinition',
9093
name: nameNode('Hello', loc(6, 11)),
9194
interfaces: [],
95+
directives: [],
9296
fields: [
9397
fieldNode(
9498
nameNode('world', loc(16, 21)),
@@ -120,6 +124,7 @@ extend type Hello {
120124
kind: 'ObjectTypeDefinition',
121125
name: nameNode('Hello', loc(13, 18)),
122126
interfaces: [],
127+
directives: [],
123128
fields: [
124129
fieldNode(
125130
nameNode('world', loc(23, 28)),
@@ -151,6 +156,7 @@ type Hello {
151156
kind: 'ObjectTypeDefinition',
152157
name: nameNode('Hello', loc(6, 11)),
153158
interfaces: [],
159+
directives: [],
154160
fields: [
155161
fieldNode(
156162
nameNode('world', loc(16, 21)),
@@ -182,6 +188,7 @@ type Hello {
182188
kind: 'ObjectTypeDefinition',
183189
name: nameNode('Hello', loc(5, 10)),
184190
interfaces: [ typeNode('World', loc(22, 27)) ],
191+
directives: [],
185192
fields: [],
186193
loc: loc(0, 31),
187194
}
@@ -205,6 +212,7 @@ type Hello {
205212
typeNode('Wo', loc(22, 24)),
206213
typeNode('rld', loc(26, 29))
207214
],
215+
directives: [],
208216
fields: [],
209217
loc: loc(0, 33),
210218
}
@@ -224,6 +232,7 @@ type Hello {
224232
{
225233
kind: 'EnumTypeDefinition',
226234
name: nameNode('Hello', loc(5, 10)),
235+
directives: [],
227236
values: [ enumValueNode('WORLD', loc(13, 18)) ],
228237
loc: loc(0, 20),
229238
}
@@ -243,6 +252,7 @@ type Hello {
243252
{
244253
kind: 'EnumTypeDefinition',
245254
name: nameNode('Hello', loc(5, 10)),
255+
directives: [],
246256
values: [
247257
enumValueNode('WO', loc(13, 15)),
248258
enumValueNode('RLD', loc(17, 20)),
@@ -268,6 +278,7 @@ interface Hello {
268278
{
269279
kind: 'InterfaceTypeDefinition',
270280
name: nameNode('Hello', loc(11, 16)),
281+
directives: [],
271282
fields: [
272283
fieldNode(
273284
nameNode('world', loc(21, 26)),
@@ -297,6 +308,7 @@ type Hello {
297308
kind: 'ObjectTypeDefinition',
298309
name: nameNode('Hello', loc(6, 11)),
299310
interfaces: [],
311+
directives: [],
300312
fields: [
301313
fieldNodeWithArgs(
302314
nameNode('world', loc(16, 21)),
@@ -334,6 +346,7 @@ type Hello {
334346
kind: 'ObjectTypeDefinition',
335347
name: nameNode('Hello', loc(6, 11)),
336348
interfaces: [],
349+
directives: [],
337350
fields: [
338351
fieldNodeWithArgs(
339352
nameNode('world', loc(16, 21)),
@@ -375,6 +388,7 @@ type Hello {
375388
kind: 'ObjectTypeDefinition',
376389
name: nameNode('Hello', loc(6, 11)),
377390
interfaces: [],
391+
directives: [],
378392
fields: [
379393
fieldNodeWithArgs(
380394
nameNode('world', loc(16, 21)),
@@ -416,6 +430,7 @@ type Hello {
416430
kind: 'ObjectTypeDefinition',
417431
name: nameNode('Hello', loc(6, 11)),
418432
interfaces: [],
433+
directives: [],
419434
fields: [
420435
fieldNodeWithArgs(
421436
nameNode('world', loc(16, 21)),
@@ -455,6 +470,7 @@ type Hello {
455470
{
456471
kind: 'UnionTypeDefinition',
457472
name: nameNode('Hello', loc(6, 11)),
473+
directives: [],
458474
types: [ typeNode('World', loc(14, 19)) ],
459475
loc: loc(0, 19),
460476
}
@@ -474,6 +490,7 @@ type Hello {
474490
{
475491
kind: 'UnionTypeDefinition',
476492
name: nameNode('Hello', loc(6, 11)),
493+
directives: [],
477494
types: [
478495
typeNode('Wo', loc(14, 16)),
479496
typeNode('Rld', loc(19, 22)),
@@ -496,6 +513,7 @@ type Hello {
496513
{
497514
kind: 'ScalarTypeDefinition',
498515
name: nameNode('Hello', loc(7, 12)),
516+
directives: [],
499517
loc: loc(0, 12),
500518
}
501519
],
@@ -517,6 +535,7 @@ input Hello {
517535
{
518536
kind: 'InputObjectTypeDefinition',
519537
name: nameNode('Hello', loc(7, 12)),
538+
directives: [],
520539
fields: [
521540
inputValueNode(
522541
nameNode('world', loc(17, 22)),

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,52 @@ type Foo implements Bar {
6565
six(argument: InputType = {key: "value"}): Type
6666
}
6767
68+
type AnnotatedObject @onObject(arg: "value") {
69+
annotatedField(arg: Type = "default" @onArg): Type @onField
70+
}
71+
6872
interface Bar {
6973
one: Type
7074
four(argument: String = "string"): String
7175
}
7276
77+
interface AnnotatedInterface @onInterface {
78+
annotatedField(arg: Type @onArg): Type @onField
79+
}
80+
7381
union Feed = Story | Article | Advert
7482
83+
union AnnotatedUnion @onUnion = A | B
84+
7585
scalar CustomScalar
7686
87+
scalar AnnotatedScalar @onScalar
88+
7789
enum Site {
7890
DESKTOP
7991
MOBILE
8092
}
8193
94+
enum AnnotatedEnum @onEnum {
95+
ANNOTATED_VALUE @onEnumValue
96+
OTHER_VALUE
97+
}
98+
8299
input InputType {
83100
key: String!
84101
answer: Int = 42
85102
}
86103
104+
input AnnotatedInput @onInputObjectType {
105+
annotatedField: Type @onField
106+
}
107+
87108
extend type Foo {
88109
seven(argument: [String]): Type
89110
}
90111
112+
extend type Foo @onType {}
113+
91114
type NoFields {}
92115
93116
directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

src/language/ast.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,15 @@ export type ScalarTypeDefinition = {
289289
kind: 'ScalarTypeDefinition';
290290
loc?: ?Location;
291291
name: Name;
292+
directives?: ?Array<Directive>;
292293
}
293294

294295
export type ObjectTypeDefinition = {
295296
kind: 'ObjectTypeDefinition';
296297
loc?: ?Location;
297298
name: Name;
298299
interfaces?: ?Array<NamedType>;
300+
directives?: ?Array<Directive>;
299301
fields: Array<FieldDefinition>;
300302
}
301303

@@ -305,6 +307,7 @@ export type FieldDefinition = {
305307
name: Name;
306308
arguments: Array<InputValueDefinition>;
307309
type: Type;
310+
directives?: ?Array<Directive>;
308311
}
309312

310313
export type InputValueDefinition = {
@@ -313,39 +316,45 @@ export type InputValueDefinition = {
313316
name: Name;
314317
type: Type;
315318
defaultValue?: ?Value;
319+
directives?: ?Array<Directive>;
316320
}
317321

318322
export type InterfaceTypeDefinition = {
319323
kind: 'InterfaceTypeDefinition';
320324
loc?: ?Location;
321325
name: Name;
326+
directives?: ?Array<Directive>;
322327
fields: Array<FieldDefinition>;
323328
}
324329

325330
export type UnionTypeDefinition = {
326331
kind: 'UnionTypeDefinition';
327332
loc?: ?Location;
328333
name: Name;
334+
directives?: ?Array<Directive>;
329335
types: Array<NamedType>;
330336
}
331337

332338
export type EnumTypeDefinition = {
333339
kind: 'EnumTypeDefinition';
334340
loc?: ?Location;
335341
name: Name;
342+
directives?: ?Array<Directive>;
336343
values: Array<EnumValueDefinition>;
337344
}
338345

339346
export type EnumValueDefinition = {
340347
kind: 'EnumValueDefinition';
341348
loc?: ?Location;
342349
name: Name;
350+
directives?: ?Array<Directive>;
343351
}
344352

345353
export type InputObjectTypeDefinition = {
346354
kind: 'InputObjectTypeDefinition';
347355
loc?: ?Location;
348356
name: Name;
357+
directives?: ?Array<Directive>;
349358
fields: Array<InputValueDefinition>;
350359
}
351360

0 commit comments

Comments
 (0)