From 9ba6b1739c5b55790b4b0d8d0d122cbc58685fe0 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Thu, 6 May 2021 00:56:58 -0700 Subject: [PATCH] Refine getNamedType() for Input and Output types (#3063) This introduces type definitions `GraphQLNamedInputType` and `GraphQLNamedOutputType` as the subsets of `GraphQLNamedType`, and adds function overrides for `getNamedType()` such that if an input or output type is provided, one of these refined subset types is returned. --- src/index.d.ts | 2 ++ src/index.js | 2 ++ src/type/definition.d.ts | 14 +++++++++++--- src/type/definition.js | 14 +++++++++++--- src/type/index.d.ts | 2 ++ src/type/index.js | 2 ++ 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index e408ae67cb..78afd0ed11 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -137,6 +137,8 @@ export { GraphQLWrappingType, GraphQLNullableType, GraphQLNamedType, + GraphQLNamedInputType, + GraphQLNamedOutputType, ThunkArray, ThunkObjMap, GraphQLSchemaConfig, diff --git a/src/index.js b/src/index.js index cd446b7fd5..30d99233d0 100644 --- a/src/index.js +++ b/src/index.js @@ -136,6 +136,8 @@ export type { GraphQLWrappingType, GraphQLNullableType, GraphQLNamedType, + GraphQLNamedInputType, + GraphQLNamedOutputType, ThunkArray, ThunkObjMap, GraphQLSchemaConfig, diff --git a/src/type/definition.d.ts b/src/type/definition.d.ts index 685d02e4d0..c520340b0e 100644 --- a/src/type/definition.d.ts +++ b/src/type/definition.d.ts @@ -249,19 +249,27 @@ export function getNullableType( /** * These named types do not include modifiers like List or NonNull. */ -export type GraphQLNamedType = +export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType; + +export type GraphQLNamedInputType = + | GraphQLScalarType + | GraphQLEnumType + | GraphQLInputObjectType; + +export type GraphQLNamedOutputType = | GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType - | GraphQLEnumType - | GraphQLInputObjectType; + | GraphQLEnumType; export function isNamedType(type: unknown): type is GraphQLNamedType; export function assertNamedType(type: unknown): GraphQLNamedType; export function getNamedType(type: undefined): undefined; +export function getNamedType(type: GraphQLInputType): GraphQLNamedInputType; +export function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType; export function getNamedType(type: GraphQLType): GraphQLNamedType; /** diff --git a/src/type/definition.js b/src/type/definition.js index 9b3f022449..7abdc25b0a 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -467,13 +467,19 @@ export function getNullableType(type) { /** * These named types do not include modifiers like List or NonNull. */ -export type GraphQLNamedType = +export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType; + +export type GraphQLNamedInputType = + | GraphQLScalarType + | GraphQLEnumType + | GraphQLInputObjectType; + +export type GraphQLNamedOutputType = | GraphQLScalarType | GraphQLObjectType | GraphQLInterfaceType | GraphQLUnionType - | GraphQLEnumType - | GraphQLInputObjectType; + | GraphQLEnumType; export function isNamedType(type: mixed): boolean %checks { return ( @@ -495,6 +501,8 @@ export function assertNamedType(type: mixed): GraphQLNamedType { /* eslint-disable no-redeclare */ declare function getNamedType(type: void | null): void; +declare function getNamedType(type: GraphQLInputType): GraphQLNamedInputType; +declare function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType; declare function getNamedType(type: GraphQLType): GraphQLNamedType; export function getNamedType(type) { /* eslint-enable no-redeclare */ diff --git a/src/type/index.d.ts b/src/type/index.d.ts index f226df5651..253fcf2113 100644 --- a/src/type/index.d.ts +++ b/src/type/index.d.ts @@ -73,6 +73,8 @@ export { GraphQLWrappingType, GraphQLNullableType, GraphQLNamedType, + GraphQLNamedInputType, + GraphQLNamedOutputType, ThunkArray, ThunkObjMap, GraphQLArgument, diff --git a/src/type/index.js b/src/type/index.js index 6bcef9daed..bf0b17973e 100644 --- a/src/type/index.js +++ b/src/type/index.js @@ -128,6 +128,8 @@ export type { GraphQLWrappingType, GraphQLNullableType, GraphQLNamedType, + GraphQLNamedInputType, + GraphQLNamedOutputType, ThunkArray, ThunkObjMap, GraphQLArgument,