Skip to content

Commit d96333d

Browse files
committed
add constraint to all GraphQl Schema Elements
1 parent f2d03c4 commit d96333d

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/type/definition.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,9 @@ export function assertAbstractType(type: unknown): GraphQLAbstractType {
365365
* })
366366
* ```
367367
*/
368-
export class GraphQLList<T extends GraphQLType> {
368+
export class GraphQLList<T extends GraphQLType>
369+
implements GraphQLSchemaElement
370+
{
369371
readonly ofType: T;
370372

371373
constructor(ofType: T) {
@@ -406,7 +408,9 @@ export class GraphQLList<T extends GraphQLType> {
406408
* ```
407409
* Note: the enforcement of non-nullability occurs within the executor.
408410
*/
409-
export class GraphQLNonNull<T extends GraphQLNullableType> {
411+
export class GraphQLNonNull<T extends GraphQLNullableType>
412+
implements GraphQLSchemaElement
413+
{
410414
readonly ofType: T;
411415

412416
constructor(ofType: T) {
@@ -530,6 +534,15 @@ export function getNamedType(
530534
}
531535
}
532536

537+
/**
538+
* An interface for all Schema Elements.
539+
*/
540+
541+
export interface GraphQLSchemaElement {
542+
toString: () => string;
543+
toJSON: () => string;
544+
}
545+
533546
/**
534547
* Used while defining GraphQL types to allow for circular references in
535548
* otherwise immutable type definitions.
@@ -635,7 +648,9 @@ export interface GraphQLScalarTypeExtensions {
635648
* `coerceInputLiteral()` method.
636649
*
637650
*/
638-
export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
651+
export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal>
652+
implements GraphQLSchemaElement
653+
{
639654
name: string;
640655
description: Maybe<string>;
641656
specifiedByURL: Maybe<string>;
@@ -851,7 +866,9 @@ export interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
851866
* });
852867
* ```
853868
*/
854-
export class GraphQLObjectType<TSource = any, TContext = any> {
869+
export class GraphQLObjectType<TSource = any, TContext = any>
870+
implements GraphQLSchemaElement
871+
{
855872
name: string;
856873
description: Maybe<string>;
857874
isTypeOf: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
@@ -1054,7 +1071,9 @@ export type GraphQLFieldConfigMap<TSource, TContext> = ObjMap<
10541071
GraphQLFieldConfig<TSource, TContext>
10551072
>;
10561073

1057-
export class GraphQLField<TSource = any, TContext = any, TArgs = any> {
1074+
export class GraphQLField<TSource = any, TContext = any, TArgs = any>
1075+
implements GraphQLSchemaElement
1076+
{
10581077
parentType:
10591078
| GraphQLObjectType<TSource, TContext>
10601079
| GraphQLInterfaceType<TSource, TContext>
@@ -1127,7 +1146,7 @@ export class GraphQLField<TSource = any, TContext = any, TArgs = any> {
11271146
}
11281147
}
11291148

1130-
export class GraphQLArgument {
1149+
export class GraphQLArgument implements GraphQLSchemaElement {
11311150
parent: GraphQLField | GraphQLDirective;
11321151
name: string;
11331152
description: Maybe<string>;
@@ -1239,7 +1258,9 @@ export interface GraphQLInterfaceTypeExtensions {
12391258
* });
12401259
* ```
12411260
*/
1242-
export class GraphQLInterfaceType<TSource = any, TContext = any> {
1261+
export class GraphQLInterfaceType<TSource = any, TContext = any>
1262+
implements GraphQLSchemaElement
1263+
{
12431264
name: string;
12441265
description: Maybe<string>;
12451266
resolveType: Maybe<GraphQLTypeResolver<TSource, TContext>>;
@@ -1366,7 +1387,7 @@ export interface GraphQLUnionTypeExtensions {
13661387
* });
13671388
* ```
13681389
*/
1369-
export class GraphQLUnionType {
1390+
export class GraphQLUnionType implements GraphQLSchemaElement {
13701391
name: string;
13711392
description: Maybe<string>;
13721393
resolveType: Maybe<GraphQLTypeResolver<any, any>>;
@@ -1483,7 +1504,7 @@ export interface GraphQLEnumTypeExtensions {
14831504
* Note: If a value is not provided in a definition, the name of the enum value
14841505
* will be used as its internal value.
14851506
*/
1486-
export class GraphQLEnumType /* <T> */ {
1507+
export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
14871508
name: string;
14881509
description: Maybe<string>;
14891510
extensions: Readonly<GraphQLEnumTypeExtensions>;
@@ -1703,7 +1724,7 @@ export interface GraphQLEnumValueConfig {
17031724
astNode?: Maybe<EnumValueDefinitionNode>;
17041725
}
17051726

1706-
export class GraphQLEnumValue {
1727+
export class GraphQLEnumValue implements GraphQLSchemaElement {
17071728
parentEnum: GraphQLEnumType;
17081729
name: string;
17091730
description: Maybe<string>;
@@ -1783,7 +1804,7 @@ export interface GraphQLInputObjectTypeExtensions {
17831804
* });
17841805
* ```
17851806
*/
1786-
export class GraphQLInputObjectType {
1807+
export class GraphQLInputObjectType implements GraphQLSchemaElement {
17871808
name: string;
17881809
description: Maybe<string>;
17891810
extensions: Readonly<GraphQLInputObjectTypeExtensions>;
@@ -1890,7 +1911,7 @@ export interface GraphQLInputFieldConfig {
18901911

18911912
export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;
18921913

1893-
export class GraphQLInputField {
1914+
export class GraphQLInputField implements GraphQLSchemaElement {
18941915
parentType: GraphQLInputObjectType;
18951916
name: string;
18961917
description: Maybe<string>;

src/type/directives.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import type { DirectiveDefinitionNode } from '../language/ast.js';
1111
import { DirectiveLocation } from '../language/directiveLocation.js';
1212

1313
import { assertName } from './assertName.js';
14-
import type { GraphQLArgumentConfig } from './definition.js';
14+
import type {
15+
GraphQLArgumentConfig,
16+
GraphQLSchemaElement,
17+
} from './definition.js';
1518
import { GraphQLArgument, GraphQLNonNull } from './definition.js';
1619
import { GraphQLBoolean, GraphQLInt, GraphQLString } from './scalars.js';
1720

@@ -48,7 +51,7 @@ export interface GraphQLDirectiveExtensions {
4851
* Directives are used by the GraphQL runtime as a way of modifying execution
4952
* behavior. Type system creators will usually not create these directly.
5053
*/
51-
export class GraphQLDirective {
54+
export class GraphQLDirective implements GraphQLSchemaElement {
5255
name: string;
5356
description: Maybe<string>;
5457
locations: ReadonlyArray<DirectiveLocation>;

0 commit comments

Comments
 (0)