Skip to content

make wrapSchema from @graphql-tools/wrap generic to handle custom TContext #3064

@AugustinLF

Description

@AugustinLF

Problem

When using a specific graphql context type in an executor of wrapSchema, TypeScript will raise an error because the types of the context and the TContext of SubschemaConfig are incompatible.

import type {CustomAppContext} from './context'

import { wrapSchema } from '@graphql-tools/wrap'
const executor: AsyncExecutor<CustomAppContext> = ...

wrapSchema({
    schema,
    executor, // TS will error here
})

The TS error is Type 'AsyncExecutor<GraphQLContext>' is not assignable to type 'Executor<Record<string, any>>'.

Solution

Updating wrapSchema to make it generic works:

import { GraphQLSchema } from 'graphql';
import { SubschemaConfig } from '@graphql-tools/delegate';
export declare function wrapSchema<K = any, V = any, C = K, TContext = Record<string, any>>(subschemaConfig: SubschemaConfig<K, V, C, TContext>): GraphQLSchema;

// let's you write
wrapSchema<any, any, any, GraphQLContext>(...) // which works

Another alternative for typings:

// I'd prefer, it makes the signature easier to grasp, the K V C is a bit confusing from an external perspective 
declare function wrapSchema<TContext = Record<string, any>>(subschemaConfig: SubschemaConfig<any, any, any, TContext>): GraphQLSchema;

I tried something like declare function wrapSchema<Config extends SubschemaConfig>(subschemaConfig: Config): GraphQLSchema; but somehow I still had the same issue.

I'll open a PR meanwhile, but feel free to let me know if I missed anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions