Skip to content

Commit

Permalink
Refine getNamedType() for Input and Output types (#3063)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leebyron authored May 6, 2021
1 parent 2673bdf commit 9ba6b17
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 @@ -137,6 +137,8 @@ export {
GraphQLWrappingType,
GraphQLNullableType,
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
ThunkArray,
ThunkObjMap,
GraphQLSchemaConfig,
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export type {
GraphQLWrappingType,
GraphQLNullableType,
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
ThunkArray,
ThunkObjMap,
GraphQLSchemaConfig,
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 @@ -249,19 +249,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: 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;

/**
Expand Down
14 changes: 11 additions & 3 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 */
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 @@ -73,6 +73,8 @@ export {
GraphQLWrappingType,
GraphQLNullableType,
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
ThunkArray,
ThunkObjMap,
GraphQLArgument,
Expand Down
2 changes: 2 additions & 0 deletions src/type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ export type {
GraphQLWrappingType,
GraphQLNullableType,
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
ThunkArray,
ThunkObjMap,
GraphQLArgument,
Expand Down

0 comments on commit 9ba6b17

Please sign in to comment.