Skip to content
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tests/**/*.ts"
],
"ignore": [
"**/generated/**"
"**/generated/**",
"tests/types/**"
],
"ignoreDependencies": [
"conventional-changelog-conventionalcommits",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"lint": "eslint src/**/*.ts",
"lint:fix": "eslint src --fix",
"deps": "docker compose up",
"test": "npm run lint && npm run test:all && npm run build",
"test": "npm run lint && npm run test:types && npm run test:all && npm run build",
"test:types": "tsc -p tsconfig.type-tests.json",
"test:all": "jest tests --no-cache --no-watchman --setupFiles dotenv/config",
"test:unit": "jest tests/unit --no-cache --no-watchman --setupFiles dotenv/config",
"test:api": "jest tests/api --no-cache --no-watchman --setupFiles dotenv/config",
Expand Down
13 changes: 9 additions & 4 deletions src/api/execute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeExecutableSchema } from '@graphql-tools/schema';
import { IFieldResolver, IResolvers } from '@graphql-tools/utils';
import { IResolvers } from '@graphql-tools/utils';
import {
DocumentNode,
GraphQLResolveInfo,
Expand All @@ -17,9 +17,14 @@ import { Context, generate, get, getResolvers } from '..';
import { Models } from '../models/models';
import { noIntrospection } from '../utils/rules';

export type ResolverWrapper = (
resolver: IFieldResolver<unknown, unknown, unknown, unknown>,
) => IFieldResolver<unknown, unknown, unknown, unknown>;
// Deliberately expressed with graphql's own `GraphQLResolveInfo` rather than `@graphql-tools/utils`'
// `IFieldResolver`: the latter augments the resolve info with members (`getAbortSignal`,
// `getAsyncHelpers`) that graphql 17 has natively but graphql 16 does not, which would make this
// type unimplementable on graphql 16 by consumers whose resolvers come from graphql-codegen.
// See tests/types/resolver-wrapper.ts.
export type FieldResolver = (source: unknown, args: unknown, context: unknown, info: GraphQLResolveInfo) => unknown;

export type ResolverWrapper = (resolver: FieldResolver) => FieldResolver;

export type ExecutorInput = {
models: Models;
Expand Down
29 changes: 29 additions & 0 deletions tests/types/resolver-wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { GraphQLResolveInfo } from 'graphql';
import type { ExecutorInput, ResolverWrapper } from '../../src';

// This file is never executed — `npm run test:types` type-checks it, and that is the assertion.
//
// graphql-codegen emits resolvers against graphql's own `GraphQLResolveInfo`, so a consumer's
// resolver wrapper is typed that way too. `ResolverWrapper` has to stay assignable from that shape
// on every graphql major our peer range allows.
//
// This regressed once: typing `ResolverWrapper` through `@graphql-tools/utils`' `IFieldResolver`
// pulled in that package's *augmented* `GraphQLResolveInfo`, which as of utils v12 requires
// `getAbortSignal` and `getAsyncHelpers`. graphql 17 declares both natively, graphql 16 declares
// neither — so on graphql 16 the assignments below stopped compiling for every consumer, while the
// jest suite stayed green (ts-jest runs transpile-only, and the one wrapper it passes is an
// inferred `(resolver) => resolver` that never pins down the parameter type).
type ResolverFn<TResult, TParent, TContext, TArgs> = (
parent: TParent,
args: TArgs,
context: TContext,
info: GraphQLResolveInfo,
) => Promise<TResult> | TResult;

declare const consumerWrapper: (
resolver: ResolverFn<unknown, unknown, unknown, unknown>,
) => ResolverFn<unknown, unknown, unknown, unknown>;

export const wrapper: ResolverWrapper = consumerWrapper;

export const executorInput: Pick<ExecutorInput, 'resolverWrapper'> = { resolverWrapper: consumerWrapper };
11 changes: 11 additions & 0 deletions tsconfig.type-tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"include": ["tests/types/**/*.ts"],
"exclude": ["node_modules", "dist"],
"compilerOptions": {
"rootDir": "./",
"noEmit": true,
"declaration": false,
"sourceMap": false
}
}