Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mutationWithClientId types with generics #389

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/mutation/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
ThunkObjMap,
} from 'graphql';

type MutationFn = (object: any, ctx: any, info: GraphQLResolveInfo) => unknown;
type MutationFn<TInput = any, TOutput = unknown, TContext = any> = (object: TInput, ctx: TContext, info: GraphQLResolveInfo) => TOutput;

/**
* A description of a mutation consumable by mutationWithClientMutationId
Expand All @@ -30,22 +30,22 @@ type MutationFn = (object: any, ctx: any, info: GraphQLResolveInfo) => unknown;
* input field, and it should return an Object with a key for each
* output field. It may return synchronously, or return a Promise.
*/
interface MutationConfig {
interface MutationConfig<TInput = any, TOutput = unknown, TContext = any> {
name: string;
description?: string;
deprecationReason?: string;
extensions?: GraphQLFieldExtensions<any, any>;
inputFields: ThunkObjMap<GraphQLInputFieldConfig>;
outputFields: ThunkObjMap<GraphQLFieldConfig<any, any>>;
mutateAndGetPayload: MutationFn;
outputFields: ThunkObjMap<GraphQLFieldConfig<TOutput, TContext>>;
mutateAndGetPayload: MutationFn<TInput, TOutput, TContext>;
}

/**
* Returns a GraphQLFieldConfig for the mutation described by the
* provided MutationConfig.
*/
export function mutationWithClientMutationId(
config: MutationConfig,
export function mutationWithClientMutationId<TInput = any, TOutput = unknown, TContext = any>(
config: MutationConfig<TInput, TOutput, TContext>,
): GraphQLFieldConfig<unknown, unknown> {
const { name, inputFields, outputFields, mutateAndGetPayload } = config;
const augmentedInputFields = () => ({
Expand Down