Skip to content

Commit

Permalink
add constraint to all GraphQl Schema Elements
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Nov 8, 2024
1 parent f2d03c4 commit d96333d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
45 changes: 33 additions & 12 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,9 @@ export function assertAbstractType(type: unknown): GraphQLAbstractType {
* })
* ```
*/
export class GraphQLList<T extends GraphQLType> {
export class GraphQLList<T extends GraphQLType>
implements GraphQLSchemaElement
{
readonly ofType: T;

constructor(ofType: T) {
Expand Down Expand Up @@ -406,7 +408,9 @@ export class GraphQLList<T extends GraphQLType> {
* ```
* Note: the enforcement of non-nullability occurs within the executor.
*/
export class GraphQLNonNull<T extends GraphQLNullableType> {
export class GraphQLNonNull<T extends GraphQLNullableType>
implements GraphQLSchemaElement
{
readonly ofType: T;

constructor(ofType: T) {
Expand Down Expand Up @@ -530,6 +534,15 @@ export function getNamedType(
}
}

/**
* An interface for all Schema Elements.
*/

export interface GraphQLSchemaElement {
toString: () => string;
toJSON: () => string;
}

/**
* Used while defining GraphQL types to allow for circular references in
* otherwise immutable type definitions.
Expand Down Expand Up @@ -635,7 +648,9 @@ export interface GraphQLScalarTypeExtensions {
* `coerceInputLiteral()` method.
*
*/
export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal> {
export class GraphQLScalarType<TInternal = unknown, TExternal = TInternal>
implements GraphQLSchemaElement
{
name: string;
description: Maybe<string>;
specifiedByURL: Maybe<string>;
Expand Down Expand Up @@ -851,7 +866,9 @@ export interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
* });
* ```
*/
export class GraphQLObjectType<TSource = any, TContext = any> {
export class GraphQLObjectType<TSource = any, TContext = any>
implements GraphQLSchemaElement
{
name: string;
description: Maybe<string>;
isTypeOf: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
Expand Down Expand Up @@ -1054,7 +1071,9 @@ export type GraphQLFieldConfigMap<TSource, TContext> = ObjMap<
GraphQLFieldConfig<TSource, TContext>
>;

export class GraphQLField<TSource = any, TContext = any, TArgs = any> {
export class GraphQLField<TSource = any, TContext = any, TArgs = any>
implements GraphQLSchemaElement
{
parentType:
| GraphQLObjectType<TSource, TContext>
| GraphQLInterfaceType<TSource, TContext>
Expand Down Expand Up @@ -1127,7 +1146,7 @@ export class GraphQLField<TSource = any, TContext = any, TArgs = any> {
}
}

export class GraphQLArgument {
export class GraphQLArgument implements GraphQLSchemaElement {
parent: GraphQLField | GraphQLDirective;
name: string;
description: Maybe<string>;
Expand Down Expand Up @@ -1239,7 +1258,9 @@ export interface GraphQLInterfaceTypeExtensions {
* });
* ```
*/
export class GraphQLInterfaceType<TSource = any, TContext = any> {
export class GraphQLInterfaceType<TSource = any, TContext = any>
implements GraphQLSchemaElement
{
name: string;
description: Maybe<string>;
resolveType: Maybe<GraphQLTypeResolver<TSource, TContext>>;
Expand Down Expand Up @@ -1366,7 +1387,7 @@ export interface GraphQLUnionTypeExtensions {
* });
* ```
*/
export class GraphQLUnionType {
export class GraphQLUnionType implements GraphQLSchemaElement {
name: string;
description: Maybe<string>;
resolveType: Maybe<GraphQLTypeResolver<any, any>>;
Expand Down Expand Up @@ -1483,7 +1504,7 @@ export interface GraphQLEnumTypeExtensions {
* Note: If a value is not provided in a definition, the name of the enum value
* will be used as its internal value.
*/
export class GraphQLEnumType /* <T> */ {
export class GraphQLEnumType /* <T> */ implements GraphQLSchemaElement {
name: string;
description: Maybe<string>;
extensions: Readonly<GraphQLEnumTypeExtensions>;
Expand Down Expand Up @@ -1703,7 +1724,7 @@ export interface GraphQLEnumValueConfig {
astNode?: Maybe<EnumValueDefinitionNode>;
}

export class GraphQLEnumValue {
export class GraphQLEnumValue implements GraphQLSchemaElement {
parentEnum: GraphQLEnumType;
name: string;
description: Maybe<string>;
Expand Down Expand Up @@ -1783,7 +1804,7 @@ export interface GraphQLInputObjectTypeExtensions {
* });
* ```
*/
export class GraphQLInputObjectType {
export class GraphQLInputObjectType implements GraphQLSchemaElement {
name: string;
description: Maybe<string>;
extensions: Readonly<GraphQLInputObjectTypeExtensions>;
Expand Down Expand Up @@ -1890,7 +1911,7 @@ export interface GraphQLInputFieldConfig {

export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;

export class GraphQLInputField {
export class GraphQLInputField implements GraphQLSchemaElement {
parentType: GraphQLInputObjectType;
name: string;
description: Maybe<string>;
Expand Down
7 changes: 5 additions & 2 deletions src/type/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import type { DirectiveDefinitionNode } from '../language/ast.js';
import { DirectiveLocation } from '../language/directiveLocation.js';

import { assertName } from './assertName.js';
import type { GraphQLArgumentConfig } from './definition.js';
import type {
GraphQLArgumentConfig,
GraphQLSchemaElement,
} from './definition.js';
import { GraphQLArgument, GraphQLNonNull } from './definition.js';
import { GraphQLBoolean, GraphQLInt, GraphQLString } from './scalars.js';

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

0 comments on commit d96333d

Please sign in to comment.