diff --git a/src/type/definition.d.ts b/src/type/definition.d.ts index b37ea16dad..457e756dd4 100644 --- a/src/type/definition.d.ts +++ b/src/type/definition.d.ts @@ -171,21 +171,16 @@ export function assertAbstractType(type: any): GraphQLAbstractType; * }) * */ -interface GraphQLList { +export class GraphQLList { readonly ofType: T; + + constructor(type: T); + toString: () => string; toJSON: () => string; inspect: () => string; } -interface _GraphQLList { - (type: T): GraphQLList; - new (type: T): GraphQLList; -} - -// eslint-disable-next-line @typescript-eslint/no-redeclare -export const GraphQLList: _GraphQLList; - /** * Non-Null Modifier * @@ -206,21 +201,16 @@ export const GraphQLList: _GraphQLList; * * Note: the enforcement of non-nullability occurs within the executor. */ -interface GraphQLNonNull { +export class GraphQLNonNull { readonly ofType: T; + + constructor(type: T); + toString: () => string; toJSON: () => string; inspect: () => string; } -interface _GraphQLNonNull { - (type: T): GraphQLNonNull; - new (type: T): GraphQLNonNull; -} - -// eslint-disable-next-line @typescript-eslint/no-redeclare -export const GraphQLNonNull: _GraphQLNonNull; - export type GraphQLWrappingType = GraphQLList | GraphQLNonNull; export function isWrappingType(type: any): type is GraphQLWrappingType; diff --git a/src/type/definition.js b/src/type/definition.js index e3a8c5c722..6ffaa4f906 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -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 (ofType: T): GraphQLList; - // 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); @@ -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 (ofType: T): GraphQLNonNull; - // 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); diff --git a/src/utilities/extendSchema.js b/src/utilities/extendSchema.js index 24e763a745..5bdf49e03b 100644 --- a/src/utilities/extendSchema.js +++ b/src/utilities/extendSchema.js @@ -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);