Skip to content

Commit

Permalink
Convert GraphQLList and GraphQLNonNull into proper classes (graphql#2906
Browse files Browse the repository at this point in the history
)
  • Loading branch information
IvanGoncharov committed Feb 20, 2021
1 parent c61bb07 commit eea7cac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 70 deletions.
26 changes: 8 additions & 18 deletions src/type/definition.d.ts
Expand Up @@ -171,21 +171,16 @@ export function assertAbstractType(type: any): GraphQLAbstractType;
* })
*
*/
interface GraphQLList<T extends GraphQLType> {
export class GraphQLList<T extends GraphQLType> {
readonly ofType: T;

constructor(type: T);

toString: () => string;
toJSON: () => string;
inspect: () => string;
}

interface _GraphQLList<T extends GraphQLType> {
(type: T): GraphQLList<T>;
new (type: T): GraphQLList<T>;
}

// eslint-disable-next-line @typescript-eslint/no-redeclare
export const GraphQLList: _GraphQLList<GraphQLType>;

/**
* Non-Null Modifier
*
Expand All @@ -206,21 +201,16 @@ export const GraphQLList: _GraphQLList<GraphQLType>;
*
* Note: the enforcement of non-nullability occurs within the executor.
*/
interface GraphQLNonNull<T extends GraphQLNullableType> {
export class GraphQLNonNull<T extends GraphQLNullableType> {
readonly ofType: T;

constructor(type: T);

toString: () => string;
toJSON: () => string;
inspect: () => string;
}

interface _GraphQLNonNull<T extends GraphQLNullableType> {
(type: T): GraphQLNonNull<T>;
new (type: T): GraphQLNonNull<T>;
}

// eslint-disable-next-line @typescript-eslint/no-redeclare
export const GraphQLNonNull: _GraphQLNonNull<GraphQLNullableType>;

export type GraphQLWrappingType = GraphQLList<any> | GraphQLNonNull<any>;

export function isWrappingType(type: any): type is GraphQLWrappingType;
Expand Down
88 changes: 36 additions & 52 deletions src/type/definition.js
Expand Up @@ -345,39 +345,31 @@ export function assertAbstractType(type: mixed): GraphQLAbstractType {
* })
*
*/
// FIXME: workaround to fix issue with Babel parser
/* ::
declare class GraphQLList<+T: GraphQLType> {
export class GraphQLList<+T: GraphQLType> {
+ofType: T;
static <T>(ofType: T): GraphQLList<T>;
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: GraphQLType): void;
}
*/

export function GraphQLList(ofType) {
// istanbul ignore else (to be removed in v16.0.0)
if (this instanceof GraphQLList) {
this.ofType = assertType(ofType);
} else {
return new GraphQLList(ofType);
constructor(ofType: T) {
devAssert(
isType(ofType),
`Expected ${inspect(ofType)} to be a GraphQL type.`,
);

this.ofType = ofType;
}
}

// Need to cast through any to alter the prototype.
(GraphQLList.prototype: any).toString = function toString() {
return '[' + String(this.ofType) + ']';
};
toString(): string {
return '[' + String(this.ofType) + ']';
}

(GraphQLList.prototype: any).toJSON = function toJSON() {
return this.toString();
};
toJSON(): string {
return this.toString();
}

Object.defineProperty(GraphQLList.prototype, SYMBOL_TO_STRING_TAG, {
get() {
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLList';
},
});
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLList);
Expand All @@ -402,39 +394,31 @@ defineInspect(GraphQLList);
*
* Note: the enforcement of non-nullability occurs within the executor.
*/
// FIXME: workaround to fix issue with Babel parser
/* ::
declare class GraphQLNonNull<+T: GraphQLNullableType> {
export class GraphQLNonNull<+T: GraphQLNullableType> {
+ofType: T;
static <T>(ofType: T): GraphQLNonNull<T>;
// Note: constructors cannot be used for covariant types. Drop the "new".
constructor(ofType: GraphQLType): void;
}
*/

export function GraphQLNonNull(ofType) {
// istanbul ignore else (to be removed in v16.0.0)
if (this instanceof GraphQLNonNull) {
this.ofType = assertNullableType(ofType);
} else {
return new GraphQLNonNull(ofType);
constructor(ofType: T) {
devAssert(
isNullableType(ofType),
`Expected ${inspect(ofType)} to be a GraphQL nullable type.`,
);

this.ofType = ofType;
}
}

// Need to cast through any to alter the prototype.
(GraphQLNonNull.prototype: any).toString = function toString() {
return String(this.ofType) + '!';
};
toString(): string {
return String(this.ofType) + '!';
}

(GraphQLNonNull.prototype: any).toJSON = function toJSON() {
return this.toString();
};
toJSON(): string {
return this.toString();
}

Object.defineProperty(GraphQLNonNull.prototype, SYMBOL_TO_STRING_TAG, {
get() {
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return 'GraphQLNonNull';
},
});
}
}

// Print a simplified form when appearing in `inspect` and `util.inspect`.
defineInspect(GraphQLNonNull);
Expand Down
1 change: 1 addition & 0 deletions src/utilities/extendSchema.js
Expand Up @@ -432,6 +432,7 @@ export function extendSchemaImpl(
return new GraphQLList(getWrappedType(node.type));
}
if (node.kind === Kind.NON_NULL_TYPE) {
// $FlowFixMe[incompatible-call]
return new GraphQLNonNull(getWrappedType(node.type));
}
return getNamedType(node);
Expand Down

0 comments on commit eea7cac

Please sign in to comment.