Skip to content

Feat: Expose user-defined directives via introspection #3047

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

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from
Draft
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,941 changes: 12 additions & 6,929 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/__tests__/starWarsIntrospection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('Star Wars Introspection Tests', () => {
{ name: '__EnumValue' },
{ name: '__Directive' },
{ name: '__DirectiveLocation' },
{ name: '__AppliedDirective' },
],
},
});
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export {
__Schema,
__Directive,
__DirectiveLocation,
__AppliedDirective,
__Type,
__Field,
__InputValue,
Expand Down
85 changes: 85 additions & 0 deletions src/type/__tests__/introspection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import { graphqlSync } from '../../graphql';

describe('Introspection', () => {
it('executes an introspection query', () => {
// TODO Include custom directives in the introspection schema
// const schema = buildSchema(`
// directive @foo(bar: Int) on FIELD_DEFINITION

// type SomeObject {
// someField: String @foo(bar: "some value")
// }

// schema {
// query: SomeObject
// }
// `);
const schema = buildSchema(`
type SomeObject {
someField: String
Expand Down Expand Up @@ -173,6 +185,30 @@ describe('Introspection', () => {
isDeprecated: false,
deprecationReason: null,
},

{
name: 'appliedDirectives',
args: [],
type: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'LIST',
name: null,
ofType: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'OBJECT',
name: '__AppliedDirective',
ofType: null,
},
},
},
},
isDeprecated: false,
deprecationReason: null,
},
],
inputFields: null,
interfaces: [],
Expand Down Expand Up @@ -892,6 +928,55 @@ describe('Introspection', () => {
],
possibleTypes: null,
},
{
kind: 'OBJECT',
name: '__AppliedDirective',
specifiedByUrl: null,
fields: [
{
name: 'name',
args: [],
type: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'SCALAR',
name: 'String',
ofType: null,
},
},
isDeprecated: false,
deprecationReason: null,
},
{
name: 'args',
args: [],
type: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'LIST',
name: null,
ofType: {
kind: 'NON_NULL',
name: null,
ofType: {
kind: 'OBJECT',
name: '__InputValue',
ofType: null,
},
},
},
},
isDeprecated: false,
deprecationReason: null,
},
],
inputFields: null,
interfaces: [],
enumValues: null,
possibleTypes: null,
},
],
directives: [
{
Expand Down
1 change: 1 addition & 0 deletions src/type/__tests__/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ describe('Type System: Schema', () => {
'__EnumValue',
'__Directive',
'__DirectiveLocation',
'__AppliedDirective',
]);

// Also check that this order is stable
Expand Down
1 change: 1 addition & 0 deletions src/type/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export {
__Schema,
__Directive,
__DirectiveLocation,
__AppliedDirective,
__Type,
__Field,
__InputValue,
Expand Down
1 change: 1 addition & 0 deletions src/type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export {
__Schema,
__Directive,
__DirectiveLocation,
__AppliedDirective,
__Type,
__Field,
__InputValue,
Expand Down
1 change: 1 addition & 0 deletions src/type/introspection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
export const __Schema: GraphQLObjectType;
export const __Directive: GraphQLObjectType;
export const __DirectiveLocation: GraphQLEnumType;
export const __AppliedDirective: GraphQLObjectType;
export const __Type: GraphQLObjectType;
export const __Field: GraphQLObjectType;
export const __InputValue: GraphQLObjectType;
Expand Down
32 changes: 31 additions & 1 deletion src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ export const __Schema: GraphQLObjectType = new GraphQLObjectType({
resolve: (schema) => schema.getSubscriptionType(),
},
directives: {
description: 'A list of all directives supported by this server.',
description:
'A list of all built in directives supported by this server.',
type: new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(__Directive)),
),
resolve: (schema) => schema.getDirectives(),
},
appliedDirectives: {
description:
'A list of all custom directives supported by this server.',
type: new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(__AppliedDirective)),
),
resolve: (schema) => schema.getAppliedDirectives(),
},
}: GraphQLFieldConfigMap<GraphQLSchema, mixed>),
});

Expand Down Expand Up @@ -193,6 +202,25 @@ export const __DirectiveLocation: GraphQLEnumType = new GraphQLEnumType({
},
});

export const __AppliedDirective: GraphQLObjectType = new GraphQLObjectType({
name: '__AppliedDirective',
description:
'An AppliedDirective is a custom directive. TODO flesh out description',
fields: () =>
({
name: {
type: new GraphQLNonNull(GraphQLString),
resolve: (appliedDirective) => appliedDirective.name,
},
args: {
type: new GraphQLNonNull(
new GraphQLList(new GraphQLNonNull(__InputValue)),
),
resolve: (appliedDirective) => appliedDirective.args,
},
}: GraphQLFieldConfigMap<GraphQLDirective, mixed>),
});

export const __Type: GraphQLObjectType = new GraphQLObjectType({
name: '__Type',
description:
Expand Down Expand Up @@ -533,6 +561,8 @@ export const introspectionTypes: $ReadOnlyArray<GraphQLNamedType> = Object.freez
__Schema,
__Directive,
__DirectiveLocation,
__AppliedDirective,
// __DirectiveArgument,
__Type,
__Field,
__InputValue,
Expand Down
8 changes: 8 additions & 0 deletions src/type/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class GraphQLSchema {
_mutationType: ?GraphQLObjectType;
_subscriptionType: ?GraphQLObjectType;
_directives: $ReadOnlyArray<GraphQLDirective>;
_appliedDirectives: $ReadOnlyArray<GraphQLDirective>;
_typeMap: TypeMap;
_subTypeMap: ObjMap<ObjMap<boolean>>;
_implementationsMap: ObjMap<{|
Expand Down Expand Up @@ -165,6 +166,8 @@ export class GraphQLSchema {
// Provide specified directives (e.g. @include and @skip) by default.
this._directives = config.directives ?? specifiedDirectives;

this._appliedDirectives = config.appliedDirectives || [];

// To preserve order of user-provided types, we add first to add them to
// the set of "collected" types, so `collectReferencedTypes` ignore them.
const allReferencedTypes: Set<GraphQLNamedType> = new Set(config.types);
Expand Down Expand Up @@ -323,6 +326,10 @@ export class GraphQLSchema {
return this._directives;
}

getAppliedDirectives(): $ReadOnlyArray<GraphQLDirective> {
return this._appliedDirectives;
}

getDirective(name: string): ?GraphQLDirective {
return this.getDirectives().find((directive) => directive.name === name);
}
Expand Down Expand Up @@ -368,6 +375,7 @@ export type GraphQLSchemaConfig = {|
subscription?: ?GraphQLObjectType,
types?: ?Array<GraphQLNamedType>,
directives?: ?Array<GraphQLDirective>,
appliedDirectives?: ?Array<GraphQLDirective>,
extensions?: ?ReadOnlyObjMapLike<mixed>,
astNode?: ?SchemaDefinitionNode,
extensionASTNodes?: ?$ReadOnlyArray<SchemaExtensionNode>,
Expand Down
11 changes: 10 additions & 1 deletion src/utilities/__tests__/printSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,11 @@ describe('Type System Printer', () => {
"""
subscriptionType: __Type

"""A list of all directives supported by this server."""
"""A list of all built in directives supported by this server."""
directives: [__Directive!]!

"""A list of all custom directives supported by this server."""
appliedDirectives: [__AppliedDirective!]!
}

"""
Expand Down Expand Up @@ -830,6 +833,12 @@ describe('Type System Printer', () => {
"""Location adjacent to an input object field definition."""
INPUT_FIELD_DEFINITION
}

"""An AppliedDirective is a custom directive. TODO flesh out description"""
type __AppliedDirective {
name: String!
args: [__InputValue!]!
}
`);
});
});