Skip to content

Commit d1027b5

Browse files
leebyronbenjiemagicmarkyaacovCR
committed
Schema Coordinates for Metafields, extracted from graphql#3044
The original schema coordinates PR on v17 (graphql#3044), since backported to 16.x.x, had non-specified support for schema coordinates for metafields. Co-authored-by: Benjie Gillam <benjie@jemjie.com> Co-authored-by: Mark Larah <mark@larah.me> Co-authored-by: Yaacov Rydzinski <yaacovCR@gmail.com>
1 parent 4675fda commit d1027b5

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

cspell.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ words:
4040
- Coodinate
4141
- metafield
4242
- graphiql
43+
- metafield
4344
- uncoerce
4445
- uncoerced
4546

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function resolveMemberCoordinate(
185185
// 6. Otherwise:
186186
// 1. Let {fieldName} be the value of the second {Name}.
187187
const fieldName = schemaCoordinate.memberName.value;
188-
const field = type.getFields()[fieldName];
188+
const field = schema.getField(type, fieldName);
189189

190190
// 2. Return the field of {type} named {fieldName} if it exists.
191191
if (field == null) {
@@ -222,7 +222,7 @@ function resolveArgumentCoordinate(
222222
// 4. Let {fieldName} be the value of the second {Name}.
223223
// 5. Let {field} be the field of {type} named {fieldName}.
224224
const fieldName = schemaCoordinate.fieldName.value;
225-
const field = type.getFields()[fieldName];
225+
const field = schema.getField(type, fieldName);
226226

227227
// 7. Assert: {field} must exist.
228228
if (field == null) {

0 commit comments

Comments
 (0)