Skip to content

Commit

Permalink
Refine getNamedType() for Input and Output types (#3410)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
IvanGoncharov and leebyron authored Dec 6, 2021
1 parent 865e534 commit c55ac60
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export {
GraphQLNullableType,
GraphQLNamedType,
Thunk,
GraphQLNamedInputType,
GraphQLNamedOutputType,
GraphQLSchemaConfig,
GraphQLSchemaExtensions,
GraphQLDirectiveConfig,
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export type {
GraphQLNullableType,
GraphQLNamedType,
Thunk,
GraphQLNamedInputType,
GraphQLNamedOutputType,
GraphQLSchemaConfig,
GraphQLDirectiveConfig,
GraphQLArgument,
Expand Down
14 changes: 11 additions & 3 deletions src/type/definition.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,27 @@ export function getNullableType<T extends GraphQLNullableType>(
/**
* 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;

/**
Expand Down
14 changes: 11 additions & 3 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions src/type/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export {
GraphQLNullableType,
GraphQLNamedType,
Thunk,
GraphQLNamedInputType,
GraphQLNamedOutputType,
GraphQLArgument,
GraphQLArgumentConfig,
GraphQLArgumentExtensions,
Expand Down
2 changes: 2 additions & 0 deletions src/type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export type {
GraphQLNullableType,
GraphQLNamedType,
Thunk,
GraphQLNamedInputType,
GraphQLNamedOutputType,
GraphQLArgument,
GraphQLArgumentConfig,
GraphQLEnumTypeConfig,
Expand Down

0 comments on commit c55ac60

Please sign in to comment.