Skip to content

Commit c55ac60

Browse files
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 <lee@leebyron.com>
1 parent 865e534 commit c55ac60

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ export {
138138
GraphQLNullableType,
139139
GraphQLNamedType,
140140
Thunk,
141+
GraphQLNamedInputType,
142+
GraphQLNamedOutputType,
141143
GraphQLSchemaConfig,
142144
GraphQLSchemaExtensions,
143145
GraphQLDirectiveConfig,

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ export type {
137137
GraphQLNullableType,
138138
GraphQLNamedType,
139139
Thunk,
140+
GraphQLNamedInputType,
141+
GraphQLNamedOutputType,
140142
GraphQLSchemaConfig,
141143
GraphQLDirectiveConfig,
142144
GraphQLArgument,

src/type/definition.d.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,19 +254,27 @@ export function getNullableType<T extends GraphQLNullableType>(
254254
/**
255255
* These named types do not include modifiers like List or NonNull.
256256
*/
257-
export type GraphQLNamedType =
257+
export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType;
258+
259+
export type GraphQLNamedInputType =
260+
| GraphQLScalarType
261+
| GraphQLEnumType
262+
| GraphQLInputObjectType;
263+
264+
export type GraphQLNamedOutputType =
258265
| GraphQLScalarType
259266
| GraphQLObjectType
260267
| GraphQLInterfaceType
261268
| GraphQLUnionType
262-
| GraphQLEnumType
263-
| GraphQLInputObjectType;
269+
| GraphQLEnumType;
264270

265271
export function isNamedType(type: any): type is GraphQLNamedType;
266272

267273
export function assertNamedType(type: any): GraphQLNamedType;
268274

269275
export function getNamedType(type: undefined): undefined;
276+
export function getNamedType(type: GraphQLInputType): GraphQLNamedInputType;
277+
export function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType;
270278
export function getNamedType(type: GraphQLType): GraphQLNamedType;
271279

272280
/**

src/type/definition.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,19 @@ export function getNullableType(type) {
493493
/**
494494
* These named types do not include modifiers like List or NonNull.
495495
*/
496-
export type GraphQLNamedType =
496+
export type GraphQLNamedType = GraphQLNamedInputType | GraphQLNamedOutputType;
497+
498+
export type GraphQLNamedInputType =
499+
| GraphQLScalarType
500+
| GraphQLEnumType
501+
| GraphQLInputObjectType;
502+
503+
export type GraphQLNamedOutputType =
497504
| GraphQLScalarType
498505
| GraphQLObjectType
499506
| GraphQLInterfaceType
500507
| GraphQLUnionType
501-
| GraphQLEnumType
502-
| GraphQLInputObjectType;
508+
| GraphQLEnumType;
503509

504510
export function isNamedType(type: mixed): boolean %checks {
505511
return (
@@ -521,6 +527,8 @@ export function assertNamedType(type: mixed): GraphQLNamedType {
521527

522528
/* eslint-disable no-redeclare */
523529
declare function getNamedType(type: void | null): void;
530+
declare function getNamedType(type: GraphQLInputType): GraphQLNamedInputType;
531+
declare function getNamedType(type: GraphQLOutputType): GraphQLNamedOutputType;
524532
declare function getNamedType(type: GraphQLType): GraphQLNamedType;
525533
export function getNamedType(type) {
526534
/* eslint-enable no-redeclare */

src/type/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export {
7474
GraphQLNullableType,
7575
GraphQLNamedType,
7676
Thunk,
77+
GraphQLNamedInputType,
78+
GraphQLNamedOutputType,
7779
GraphQLArgument,
7880
GraphQLArgumentConfig,
7981
GraphQLArgumentExtensions,

src/type/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export type {
129129
GraphQLNullableType,
130130
GraphQLNamedType,
131131
Thunk,
132+
GraphQLNamedInputType,
133+
GraphQLNamedOutputType,
132134
GraphQLArgument,
133135
GraphQLArgumentConfig,
134136
GraphQLEnumTypeConfig,

0 commit comments

Comments
 (0)