Skip to content

Commit 69b4fe4

Browse files
leebyronbenjiemagicmark
authored andcommitted
Schema Coordinates (#3044)
This PR is the residual changes from the original schema coordinates PR above and beyond what was backported to 16.x.x Co-authored-by: Benjie Gillam <benjie@jemjie.com> Co-authored-by: Mark Larah <mark@larah.me>
1 parent bdeea27 commit 69b4fe4

7 files changed

Lines changed: 87 additions & 32 deletions

File tree

cspell.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ overrides:
2121
- html
2222
words:
2323
- craco
24+
- esbuild
2425
- swcrc
2526
- codegen
2627
- subschema
@@ -30,16 +31,14 @@ overrides:
3031
- URQL
3132
- tada
3233
- Graphile
33-
- esbuild
3434

3535
validateDirectives: true
3636
ignoreRegExpList:
3737
- u\{[0-9a-f]{1,8}\}
3838

3939
words:
40-
- Coodinate
41-
- metafield
4240
- graphiql
41+
- metafield
4342
- uncoerce
4443
- uncoerced
4544

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ export type {
330330
UnionTypeExtensionNode,
331331
EnumTypeExtensionNode,
332332
InputObjectTypeExtensionNode,
333-
// Schema Coordinates
334333
SchemaCoordinateNode,
335334
TypeCoordinateNode,
336335
MemberCoordinateNode,

src/language/__tests__/predicates-test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,12 @@ describe('AST node predicates', () => {
144144
});
145145

146146
it('isSchemaCoordinateNode', () => {
147-
expect(
148-
[
149-
Kind.TYPE_COORDINATE,
150-
Kind.MEMBER_COORDINATE,
151-
Kind.ARGUMENT_COORDINATE,
152-
Kind.DIRECTIVE_COORDINATE,
153-
Kind.DIRECTIVE_ARGUMENT_COORDINATE,
154-
].every((kind) => isSchemaCoordinateNode({ kind } as ASTNode)),
155-
).to.equal(true);
147+
expect(filterNodes(isSchemaCoordinateNode)).to.deep.equal([
148+
'ArgumentCoordinate',
149+
'DirectiveArgumentCoordinate',
150+
'DirectiveCoordinate',
151+
'MemberCoordinate',
152+
'TypeCoordinate',
153+
]);
156154
});
157155
});

src/language/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export type {
9696
UnionTypeExtensionNode,
9797
EnumTypeExtensionNode,
9898
InputObjectTypeExtensionNode,
99-
// Schema Coordinates
10099
SchemaCoordinateNode,
101100
TypeCoordinateNode,
102101
MemberCoordinateNode,

src/language/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,15 +1491,15 @@ export class Parser {
14911491
throw this.unexpected(start);
14921492
}
14931493

1494-
// Schema Coordinates
1494+
// Implements the parsing rules in the Schema Coordinates section.
14951495

14961496
/**
14971497
* SchemaCoordinate :
14981498
* - Name
14991499
* - Name . Name
15001500
* - Name . Name ( Name : )
1501-
* - \@ Name
1502-
* - \@ Name ( Name : )
1501+
* - @ Name
1502+
* - @ Name ( Name : )
15031503
*/
15041504
parseSchemaCoordinate(): SchemaCoordinateNode {
15051505
const start = this._lexer.token;

src/utilities/__tests__/resolveSchemaCoordinate-test.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { expect } from 'chai';
1+
import { assert, expect } from 'chai';
22
import { describe, it } from 'mocha';
33

44
import type {
55
GraphQLEnumType,
6+
GraphQLField,
67
GraphQLInputObjectType,
78
GraphQLObjectType,
89
} from '../../type/definition.js';
@@ -181,3 +182,68 @@ describe('resolveSchemaCoordinate', () => {
181182
);
182183
});
183184
});
185+
186+
/*
187+
* NOTE: the following are not required for spec compliance; resolution
188+
* of meta-fields is implementation-defined.
189+
*
190+
* These tests are here to ensure a change of behavior will only be made
191+
* in a semver-major release of GraphQL.js.
192+
*/
193+
describe('resolveSchemaCoordinate (meta-fields and introspection types)', () => {
194+
it('resolves a meta-field', () => {
195+
const type = schema.getType('Business') as GraphQLObjectType;
196+
const field = schema.getField(type, '__typename');
197+
assert.ok(field);
198+
expect(
199+
resolveSchemaCoordinate(schema, 'Business.__typename'),
200+
).to.deep.equal({
201+
kind: 'Field',
202+
type,
203+
field,
204+
});
205+
});
206+
207+
it('resolves a meta-field argument', () => {
208+
const type = schema.getType('Query') as GraphQLObjectType;
209+
const field = schema.getField(type, '__type') as GraphQLField;
210+
const fieldArgument = field.args.find((arg) => arg.name === 'name');
211+
expect(
212+
resolveSchemaCoordinate(schema, 'Query.__type(name:)'),
213+
).to.deep.equal({
214+
kind: 'FieldArgument',
215+
type,
216+
field,
217+
fieldArgument,
218+
});
219+
});
220+
221+
it('resolves an Introspection Type', () => {
222+
expect(resolveSchemaCoordinate(schema, '__Type')).to.deep.equal({
223+
kind: 'NamedType',
224+
type: schema.getType('__Type'),
225+
});
226+
});
227+
228+
it('resolves an Introspection Type Field', () => {
229+
const type = schema.getType('__Directive') as GraphQLObjectType;
230+
const field = type.getFields().name;
231+
expect(resolveSchemaCoordinate(schema, '__Directive.name')).to.deep.equal({
232+
kind: 'Field',
233+
type,
234+
field,
235+
});
236+
});
237+
238+
it('resolves an Introspection Type Enum Value', () => {
239+
const type = schema.getType('__DirectiveLocation') as GraphQLEnumType;
240+
const enumValue = type.getValue('INLINE_FRAGMENT');
241+
expect(
242+
resolveSchemaCoordinate(schema, '__DirectiveLocation.INLINE_FRAGMENT'),
243+
).to.deep.equal({
244+
kind: 'EnumValue',
245+
type,
246+
enumValue,
247+
});
248+
});
249+
});

src/utilities/resolveSchemaCoordinate.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ function resolveMemberCoordinate(
148148
!isInterfaceType(type)
149149
) {
150150
throw new Error(
151-
`Expected ${inspect(
152-
typeName,
153-
)} to be an Enum, Input Object, Object or Interface type.`,
151+
`Expected ${inspect(typeName)} to be an Enum, Input Object, Object or Interface type.`,
154152
);
155153
}
156154

@@ -185,7 +183,7 @@ function resolveMemberCoordinate(
185183
// 6. Otherwise:
186184
// 1. Let {fieldName} be the value of the second {Name}.
187185
const fieldName = schemaCoordinate.memberName.value;
188-
const field = type.getFields()[fieldName];
186+
const field = schema.getField(type, fieldName);
189187

190188
// 2. Return the field of {type} named {fieldName} if it exists.
191189
if (field == null) {
@@ -222,21 +220,19 @@ function resolveArgumentCoordinate(
222220
// 4. Let {fieldName} be the value of the second {Name}.
223221
// 5. Let {field} be the field of {type} named {fieldName}.
224222
const fieldName = schemaCoordinate.fieldName.value;
225-
const field = type.getFields()[fieldName];
223+
const field = schema.getField(type, fieldName);
226224

227225
// 7. Assert: {field} must exist.
228226
if (field == null) {
229227
throw new Error(
230-
`Expected ${inspect(fieldName)} to exist as a field of type ${inspect(
231-
typeName,
232-
)} in the schema.`,
228+
`Expected ${inspect(fieldName)} to exist as a field of type ${inspect(typeName)} in the schema.`,
233229
);
234230
}
235231

236232
// 7. Let {fieldArgumentName} be the value of the third {Name}.
237233
const fieldArgumentName = schemaCoordinate.argumentName.value;
238234
const fieldArgument = field.args.find(
239-
(arg: GraphQLArgument) => arg.name === fieldArgumentName,
235+
(arg) => arg.name === fieldArgumentName,
240236
);
241237

242238
// 8. Return the argument of {field} named {fieldArgumentName} if it exists.
@@ -248,7 +244,7 @@ function resolveArgumentCoordinate(
248244
}
249245

250246
/**
251-
* DirectiveCoordinate : \@ Name
247+
* DirectiveCoordinate : @ Name
252248
*/
253249
function resolveDirectiveCoordinate(
254250
schema: GraphQLSchema,
@@ -267,7 +263,7 @@ function resolveDirectiveCoordinate(
267263
}
268264

269265
/**
270-
* DirectiveArgumentCoordinate : \@ Name ( Name : )
266+
* DirectiveArgumentCoordinate : @ Name ( Name : )
271267
*/
272268
function resolveDirectiveArgumentCoordinate(
273269
schema: GraphQLSchema,
@@ -281,9 +277,7 @@ function resolveDirectiveArgumentCoordinate(
281277
// 3. Assert {directive} must exist.
282278
if (!directive) {
283279
throw new Error(
284-
`Expected ${inspect(
285-
directiveName,
286-
)} to be defined as a directive in the schema.`,
280+
`Expected ${inspect(directiveName)} to be defined as a directive in the schema.`,
287281
);
288282
}
289283

0 commit comments

Comments
 (0)