Skip to content

Commit 3e916ef

Browse files
authored
feat: convert Thunk to ThunkArray and ThunkObjMap (#2955)
1 parent 789a98a commit 3e916ef

File tree

7 files changed

+47
-31
lines changed

7 files changed

+47
-31
lines changed

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ export {
137137
GraphQLWrappingType,
138138
GraphQLNullableType,
139139
GraphQLNamedType,
140-
Thunk,
140+
ThunkArray,
141+
ThunkObjMap,
141142
GraphQLSchemaConfig,
142143
GraphQLSchemaExtensions,
143144
GraphQLDirectiveConfig,

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ export type {
136136
GraphQLWrappingType,
137137
GraphQLNullableType,
138138
GraphQLNamedType,
139-
Thunk,
139+
ThunkArray,
140+
ThunkObjMap,
140141
GraphQLSchemaConfig,
141142
GraphQLDirectiveConfig,
142143
GraphQLArgument,

src/jsutils/ObjMap.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type ObjMap<T> = Record<string, T>;
2+
export type ObjMapLike<T> = ObjMap<T> | Record<string, T>;
3+
4+
export type ReadOnlyObjMap<T> = Readonly<Record<string, T>>;
5+
export type ReadOnlyObjMapLike<T> =
6+
| Readonly<Record<string, T>>
7+
| ReadOnlyObjMap<T>;

src/type/definition.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Maybe } from '../jsutils/Maybe';
55

66
import { PromiseOrValue } from '../jsutils/PromiseOrValue';
77
import { Path } from '../jsutils/Path';
8+
import { ObjMap } from '../jsutils/ObjMap';
89

910
import {
1011
ScalarTypeDefinitionNode,
@@ -263,9 +264,8 @@ export function getNamedType(type: GraphQLType): GraphQLNamedType;
263264
* Used while defining GraphQL types to allow for circular references in
264265
* otherwise immutable type definitions.
265266
*/
266-
export type Thunk<T extends { [key: string]: any } | Array<any>> =
267-
| (() => T)
268-
| T;
267+
export type ThunkArray<T> = Array<T> | (() => Array<T>);
268+
export type ThunkObjMap<T> = ObjMap<T> | (() => ObjMap<T>);
269269

270270
/**
271271
* Custom extensions
@@ -435,8 +435,8 @@ export function argsToArgsConfig(
435435
export interface GraphQLObjectTypeConfig<TSource, TContext> {
436436
name: string;
437437
description?: Maybe<string>;
438-
interfaces?: Thunk<Array<GraphQLInterfaceType>>;
439-
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>;
438+
interfaces?: ThunkArray<GraphQLInterfaceType>;
439+
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
440440
isTypeOf?: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
441441
extensions?: Maybe<Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>>;
442442
astNode?: Maybe<ObjectTypeDefinitionNode>;
@@ -637,8 +637,8 @@ export class GraphQLInterfaceType {
637637
export interface GraphQLInterfaceTypeConfig<TSource, TContext> {
638638
name: string;
639639
description?: Maybe<string>;
640-
interfaces?: Thunk<Array<GraphQLInterfaceType>>;
641-
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>;
640+
interfaces?: ThunkArray<GraphQLInterfaceType>;
641+
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>;
642642
/**
643643
* Optionally provide a custom type resolver function. If one is not provided,
644644
* the default implementation will call `isTypeOf` on each implementing
@@ -711,7 +711,7 @@ export class GraphQLUnionType {
711711
export interface GraphQLUnionTypeConfig<TSource, TContext> {
712712
name: string;
713713
description?: Maybe<string>;
714-
types: Thunk<Array<GraphQLObjectType>>;
714+
types: ThunkArray<GraphQLObjectType>;
715715
/**
716716
* Optionally provide a custom type resolver function. If one is not provided,
717717
* the default implementation will call `isTypeOf` on each implementing
@@ -884,7 +884,7 @@ export class GraphQLInputObjectType {
884884
export interface GraphQLInputObjectTypeConfig {
885885
name: string;
886886
description?: Maybe<string>;
887-
fields: Thunk<GraphQLInputFieldConfigMap>;
887+
fields: ThunkObjMap<GraphQLInputFieldConfig>;
888888
extensions?: Maybe<Readonly<GraphQLInputObjectTypeExtensions>>;
889889
astNode?: Maybe<InputObjectTypeDefinitionNode>;
890890
extensionASTNodes?: Maybe<ReadonlyArray<InputObjectTypeExtensionNode>>;

src/type/definition.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,14 @@ export function getNamedType(type) {
511511
* Used while defining GraphQL types to allow for circular references in
512512
* otherwise immutable type definitions.
513513
*/
514-
export type Thunk<+T: ObjMap<any> | Array<any>> = (() => T) | T;
514+
export type ThunkArray<T> = (() => Array<T>) | Array<T>;
515+
export type ThunkObjMap<T> = (() => ObjMap<T>) | ObjMap<T>;
515516

516-
function resolveThunk<+T: ObjMap<any> | Array<any>>(thunk: Thunk<T>): T {
517+
function resolveArrayThunk<T>(thunk: ThunkArray<T>): Array<T> {
518+
return typeof thunk === 'function' ? thunk() : thunk;
519+
}
520+
521+
function resolveObjMapThunk<T>(thunk: ThunkObjMap<T>): ObjMap<T> {
517522
return typeof thunk === 'function' ? thunk() : thunk;
518523
}
519524

@@ -703,8 +708,8 @@ export class GraphQLObjectType {
703708
astNode: ?ObjectTypeDefinitionNode;
704709
extensionASTNodes: ?$ReadOnlyArray<ObjectTypeExtensionNode>;
705710
706-
_fields: Thunk<GraphQLFieldMap<any, any>>;
707-
_interfaces: Thunk<Array<GraphQLInterfaceType>>;
711+
_fields: ThunkObjMap<GraphQLField<any, any>>;
712+
_interfaces: ThunkArray<GraphQLInterfaceType>;
708713
709714
constructor(config: $ReadOnly<GraphQLObjectTypeConfig<any, any>>) {
710715
this.name = config.name;
@@ -771,7 +776,7 @@ function defineInterfaces(
771776
| GraphQLInterfaceTypeConfig<mixed, mixed>,
772777
>,
773778
): Array<GraphQLInterfaceType> {
774-
const interfaces = resolveThunk(config.interfaces ?? []);
779+
const interfaces = resolveArrayThunk(config.interfaces ?? []);
775780
devAssert(
776781
Array.isArray(interfaces),
777782
`${config.name} interfaces must be an Array or a function which returns an Array.`,
@@ -785,7 +790,7 @@ function defineFieldMap<TSource, TContext>(
785790
| GraphQLInterfaceTypeConfig<TSource, TContext>,
786791
>,
787792
): GraphQLFieldMap<TSource, TContext> {
788-
const fieldMap = resolveThunk(config.fields);
793+
const fieldMap = resolveObjMapThunk(config.fields);
789794
devAssert(
790795
isPlainObj(fieldMap),
791796
`${config.name} fields must be an object with field names as keys or a function which returns such an object.`,
@@ -874,8 +879,8 @@ export function argsToArgsConfig(
874879
export type GraphQLObjectTypeConfig<TSource, TContext> = {|
875880
name: string,
876881
description?: ?string,
877-
interfaces?: Thunk<Array<GraphQLInterfaceType>>,
878-
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>,
882+
interfaces?: ThunkArray<GraphQLInterfaceType>,
883+
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>,
879884
isTypeOf?: ?GraphQLIsTypeOfFn<TSource, TContext>,
880885
extensions?: ?ReadOnlyObjMapLike<mixed>,
881886
astNode?: ?ObjectTypeDefinitionNode,
@@ -1020,8 +1025,8 @@ export class GraphQLInterfaceType {
10201025
astNode: ?InterfaceTypeDefinitionNode;
10211026
extensionASTNodes: ?$ReadOnlyArray<InterfaceTypeExtensionNode>;
10221027

1023-
_fields: Thunk<GraphQLFieldMap<any, any>>;
1024-
_interfaces: Thunk<Array<GraphQLInterfaceType>>;
1028+
_fields: ThunkObjMap<GraphQLField<any, any>>;
1029+
_interfaces: ThunkArray<GraphQLInterfaceType>;
10251030

10261031
constructor(config: $ReadOnly<GraphQLInterfaceTypeConfig<any, any>>) {
10271032
this.name = config.name;
@@ -1085,8 +1090,8 @@ export class GraphQLInterfaceType {
10851090
export type GraphQLInterfaceTypeConfig<TSource, TContext> = {|
10861091
name: string,
10871092
description?: ?string,
1088-
interfaces?: Thunk<Array<GraphQLInterfaceType>>,
1089-
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>,
1093+
interfaces?: ThunkArray<GraphQLInterfaceType>,
1094+
fields: ThunkObjMap<GraphQLFieldConfig<TSource, TContext>>,
10901095
/**
10911096
* Optionally provide a custom type resolver function. If one is not provided,
10921097
* the default implementation will call `isTypeOf` on each implementing
@@ -1137,7 +1142,7 @@ export class GraphQLUnionType {
11371142
astNode: ?UnionTypeDefinitionNode;
11381143
extensionASTNodes: ?$ReadOnlyArray<UnionTypeExtensionNode>;
11391144

1140-
_types: Thunk<Array<GraphQLObjectType>>;
1145+
_types: ThunkArray<GraphQLObjectType>;
11411146

11421147
constructor(config: $ReadOnly<GraphQLUnionTypeConfig<any, any>>) {
11431148
this.name = config.name;
@@ -1192,7 +1197,7 @@ export class GraphQLUnionType {
11921197
function defineTypes(
11931198
config: $ReadOnly<GraphQLUnionTypeConfig<mixed, mixed>>,
11941199
): Array<GraphQLObjectType> {
1195-
const types = resolveThunk(config.types);
1200+
const types = resolveArrayThunk(config.types);
11961201
devAssert(
11971202
Array.isArray(types),
11981203
`Must provide Array of types or a function which returns such an array for Union ${config.name}.`,
@@ -1203,7 +1208,7 @@ function defineTypes(
12031208
export type GraphQLUnionTypeConfig<TSource, TContext> = {|
12041209
name: string,
12051210
description?: ?string,
1206-
types: Thunk<Array<GraphQLObjectType>>,
1211+
types: ThunkArray<GraphQLObjectType>,
12071212
/**
12081213
* Optionally provide a custom type resolver function. If one is not provided,
12091214
* the default implementation will call `isTypeOf` on each implementing
@@ -1463,7 +1468,7 @@ export class GraphQLInputObjectType {
14631468
astNode: ?InputObjectTypeDefinitionNode;
14641469
extensionASTNodes: ?$ReadOnlyArray<InputObjectTypeExtensionNode>;
14651470

1466-
_fields: Thunk<GraphQLInputFieldMap>;
1471+
_fields: ThunkObjMap<GraphQLInputField>;
14671472

14681473
constructor(config: $ReadOnly<GraphQLInputObjectTypeConfig>) {
14691474
this.name = config.name;
@@ -1519,7 +1524,7 @@ export class GraphQLInputObjectType {
15191524
function defineInputFieldMap(
15201525
config: $ReadOnly<GraphQLInputObjectTypeConfig>,
15211526
): GraphQLInputFieldMap {
1522-
const fieldMap = resolveThunk(config.fields);
1527+
const fieldMap = resolveObjMapThunk(config.fields);
15231528
devAssert(
15241529
isPlainObj(fieldMap),
15251530
`${config.name} fields must be an object with field names as keys or a function which returns such an object.`,
@@ -1545,7 +1550,7 @@ function defineInputFieldMap(
15451550
export type GraphQLInputObjectTypeConfig = {|
15461551
name: string,
15471552
description?: ?string,
1548-
fields: Thunk<GraphQLInputFieldConfigMap>,
1553+
fields: ThunkObjMap<GraphQLInputFieldConfig>,
15491554
extensions?: ?ReadOnlyObjMapLike<mixed>,
15501555
astNode?: ?InputObjectTypeDefinitionNode,
15511556
extensionASTNodes?: ?$ReadOnlyArray<InputObjectTypeExtensionNode>,

src/type/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export {
7373
GraphQLWrappingType,
7474
GraphQLNullableType,
7575
GraphQLNamedType,
76-
Thunk,
76+
ThunkArray,
77+
ThunkObjMap,
7778
GraphQLArgument,
7879
GraphQLArgumentConfig,
7980
GraphQLArgumentExtensions,

src/type/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ export type {
128128
GraphQLWrappingType,
129129
GraphQLNullableType,
130130
GraphQLNamedType,
131-
Thunk,
131+
ThunkArray,
132+
ThunkObjMap,
132133
GraphQLArgument,
133134
GraphQLArgumentConfig,
134135
GraphQLEnumTypeConfig,

0 commit comments

Comments
 (0)