Skip to content

Commit bc01861

Browse files
committed
feat(graphql): infer return of resolve from type (only GraphQLScalarType)
See #1
1 parent 36cc105 commit bc01861

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

@types/graphql/type/definition.d.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,25 +319,35 @@ export interface GraphQLScalarTypeConfig<TInternal, TExternal> {
319319
* });
320320
*
321321
*/
322-
export class GraphQLObjectType {
322+
export class GraphQLObjectType<
323+
TSource = any,
324+
TContext = any,
325+
TConfig extends GraphQLObjectTypeConfig<TSource, TContext> = any,
326+
> {
323327
name: string;
324328
description: Maybe<string>;
325329
astNode: Maybe<ObjectTypeDefinitionNode>;
326330
extensionASTNodes: Maybe<ReadonlyArray<ObjectTypeExtensionNode>>;
327331
isTypeOf: Maybe<GraphQLIsTypeOfFn<any, any>>;
328332

329-
constructor(config: GraphQLObjectTypeConfig<any, any>);
330-
getFields(): GraphQLFieldMap<any, any>;
333+
constructor(config: TConfig & GraphQLObjectTypeConfig<TSource, TContext, Unthunk<TConfig['fields']>>);
334+
getFields(): GraphQLFieldMap<TSource, TContext>;
331335
getInterfaces(): GraphQLInterfaceType[];
332336
toString(): string;
333337
toJSON(): string;
334338
inspect(): string;
335339
}
336340

337-
export interface GraphQLObjectTypeConfig<TSource, TContext> {
341+
type Unthunk<T> = T extends () => infer R ? R : T
342+
343+
export interface GraphQLObjectTypeConfig<
344+
TSource,
345+
TContext,
346+
TFields extends GraphQLFieldConfigMap<TSource, TContext> = any
347+
> {
338348
name: string;
339349
interfaces?: Thunk<Maybe<GraphQLInterfaceType[]>>;
340-
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>;
350+
fields: Thunk<GraphQLFieldConfigMapFromConfig<TSource, TContext, TFields>>;
341351
isTypeOf?: Maybe<GraphQLIsTypeOfFn<TSource, TContext>>;
342352
description?: Maybe<string>;
343353
astNode?: Maybe<ObjectTypeDefinitionNode>;
@@ -356,12 +366,12 @@ export type GraphQLIsTypeOfFn<TSource, TContext> = (
356366
info: GraphQLResolveInfo
357367
) => MaybePromise<boolean>;
358368

359-
export type GraphQLFieldResolver<TSource, TContext, TArgs = { [argName: string]: any }> = (
369+
export type GraphQLFieldResolver<TSource, TContext, TArgs = { [argName: string]: any }, TResult = any> = (
360370
source: TSource,
361371
args: TArgs,
362372
context: TContext,
363373
info: GraphQLResolveInfo
364-
) => any;
374+
) => TResult;
365375

366376
export interface GraphQLResolveInfo {
367377
readonly fieldName: string;
@@ -381,10 +391,14 @@ export type ResponsePath = {
381391
readonly key: string | number;
382392
};
383393

384-
export interface GraphQLFieldConfig<TSource, TContext, TArgs = { [argName: string]: any }> {
385-
type: GraphQLOutputType;
394+
export type ExtractPrimitiveFromGraphQL<T> =
395+
T extends GraphQLScalarType<infer R> ? R :
396+
any
397+
398+
export interface GraphQLFieldConfig<TSource, TContext, TArgs = { [argName: string]: any }, TType extends GraphQLOutputType = GraphQLOutputType> {
399+
type: TType;
386400
args?: GraphQLFieldConfigArgumentMap;
387-
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>;
401+
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs, ExtractPrimitiveFromGraphQL<TType>>;
388402
subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs>;
389403
deprecationReason?: Maybe<string>;
390404
description?: Maybe<string>;
@@ -400,8 +414,19 @@ export interface GraphQLArgumentConfig {
400414
astNode?: Maybe<InputValueDefinitionNode>;
401415
}
402416

403-
export type GraphQLFieldConfigMap<TSource, TContext> = {
404-
[key: string]: GraphQLFieldConfig<TSource, TContext>;
417+
export type GraphQLFieldConfigMap<
418+
TSource,
419+
TContext,
420+
> = {
421+
[name: string]: GraphQLFieldConfig<TSource, TContext>;
422+
};
423+
424+
export type GraphQLFieldConfigMapFromConfig<
425+
TSource,
426+
TContext,
427+
TFields extends GraphQLFieldConfigMap<TSource, TContext> = any,
428+
> = {
429+
[K in keyof TFields]: GraphQLFieldConfig<TSource, TContext, any, TFields[K]['type']>;
405430
};
406431

407432
export interface GraphQLField<TSource, TContext, TArgs = { [key: string]: any }> {

0 commit comments

Comments
 (0)