From c55ac609582a1e2eabfad737e3704aa7fc231db3 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 6 Dec 2021 19:00:25 +0200 Subject: [PATCH] Refine getNamedType() for Input and Output types (#3410) Backport of #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. Co-authored-by: Lee Byron --- 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 13a465b4e1..e0707adeaa 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -138,6 +138,8 @@ export { GraphQLNullableType, GraphQLNamedType, Thunk, + GraphQLNamedInputType, + GraphQLNamedOutputType, GraphQLSchemaConfig, GraphQLSchemaExtensions, GraphQLDirectiveConfig, diff --git a/src/index.js b/src/index.js index 104ab88658..4387ce14fa 100644 --- a/src/index.js +++ b/src/index.js @@ -137,6 +137,8 @@ export type { GraphQLNullableType, GraphQLNamedType, Thunk, + GraphQLNamedInputType, + GraphQLNamedOutputType, GraphQLSchemaConfig, GraphQLDirectiveConfig, GraphQLArgument, diff --git a/src/type/definition.d.ts b/src/type/definition.d.ts index 06799981f9..b304d226cf 100644 --- a/src/type/definition.d.ts +++ b/src/type/definition.d.ts @@ -254,19 +254,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: any): type is GraphQLNamedType; export function assertNamedType(type: any): 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 2b762fb79d..9b944d3fd1 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -493,13 +493,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 ( @@ -521,6 +527,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 9686f413b7..363531985e 100644 --- a/src/type/index.d.ts +++ b/src/type/index.d.ts @@ -74,6 +74,8 @@ export { GraphQLNullableType, GraphQLNamedType, Thunk, + GraphQLNamedInputType, + GraphQLNamedOutputType, GraphQLArgument, GraphQLArgumentConfig, GraphQLArgumentExtensions, diff --git a/src/type/index.js b/src/type/index.js index 811d50247a..8aa50d678b 100644 --- a/src/type/index.js +++ b/src/type/index.js @@ -129,6 +129,8 @@ export type { GraphQLNullableType, GraphQLNamedType, Thunk, + GraphQLNamedInputType, + GraphQLNamedOutputType, GraphQLArgument, GraphQLArgumentConfig, GraphQLEnumTypeConfig,