Skip to content

Commit 0bbb176

Browse files
[email protected] readiness (refine generic typings via extends) (#4382)
* Install [email protected] and fix one of the TS errors * Resolve remaining TS errors * Fix ambiguous typings * Revert TS version back to latest * Add changeset
1 parent 1179697 commit 0bbb176

20 files changed

+184
-136
lines changed

.changeset/slow-singers-pull.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@graphql-tools/delegate': patch
3+
'@graphql-tools/links': patch
4+
'@graphql-tools/git-loader': patch
5+
'@graphql-tools/url-loader': patch
6+
'@graphql-tools/stitch': patch
7+
'@graphql-tools/utils': patch
8+
'@graphql-tools/wrap': patch
9+
---
10+
11+
Refine generic typings using `extends X` when appropriate
12+
13+
Typescript 4.7 has stricter requirements around generics
14+
which is explained well in the related PR:
15+
https://github.com/microsoft/TypeScript/pull/48366
16+
17+
These changes resolve the errors that these packages will
18+
face when attempting to upgrade to TS 4.7 (still in beta
19+
at the time of writing this). Landing these changes now
20+
will allow other TS libraries which depend on these
21+
packages to experiment with TS 4.7 in the meantime.

packages/delegate/src/Transformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Transformation<TContext> {
1111
context: Record<string, any>;
1212
}
1313

14-
export class Transformer<TContext = Record<string, any>> {
14+
export class Transformer<TContext extends Record<string, any> = Record<string, any>> {
1515
private transformations: Array<Transformation<TContext>> = [];
1616
private delegationContext: DelegationContext<TContext>;
1717

@@ -51,7 +51,7 @@ export class Transformer<TContext = Record<string, any>> {
5151
public transformResult(originalResult: ExecutionResult) {
5252
let result = originalResult;
5353

54-
// from rigth to left
54+
// from right to left
5555
for (let i = this.transformations.length - 1; i >= 0; i--) {
5656
const transformation = this.transformations[i];
5757
if (transformation.transform.transformResult) {

packages/delegate/src/checkResultAndHandleErrors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AggregateError, getResponseKeyFromInfo, ExecutionResult, relocatedError
55
import { DelegationContext } from './types';
66
import { resolveExternalValue } from './resolveExternalValue';
77

8-
export function checkResultAndHandleErrors<TContext>(
8+
export function checkResultAndHandleErrors<TContext extends Record<string, any>>(
99
result: ExecutionResult,
1010
delegationContext: DelegationContext<TContext>
1111
): any {

packages/delegate/src/delegateToSchema.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ import { Subschema } from './Subschema';
3838
import { createRequest, getDelegatingOperation } from './createRequest';
3939
import { Transformer } from './Transformer';
4040

41-
export function delegateToSchema<TContext = Record<string, any>, TArgs = any>(
42-
options: IDelegateToSchemaOptions<TContext, TArgs>
43-
): any {
41+
export function delegateToSchema<
42+
TContext extends Record<string, any> = Record<string, any>,
43+
TArgs extends Record<string, any> = any
44+
>(options: IDelegateToSchemaOptions<TContext, TArgs>): any {
4445
const {
4546
info,
4647
schema,
@@ -85,9 +86,10 @@ function getDelegationReturnType(
8586
return rootType.getFields()[fieldName].type;
8687
}
8788

88-
export function delegateRequest<TContext = Record<string, any>, TArgs = any>(
89-
options: IDelegateRequestOptions<TContext, TArgs>
90-
) {
89+
export function delegateRequest<
90+
TContext extends Record<string, any> = Record<string, any>,
91+
TArgs extends Record<string, any> = any
92+
>(options: IDelegateRequestOptions<TContext, TArgs>) {
9193
const delegationContext = getDelegationContext(options);
9294

9395
const transformer = new Transformer<TContext>(delegationContext);
@@ -112,7 +114,7 @@ export function delegateRequest<TContext = Record<string, any>, TArgs = any>(
112114
.resolve();
113115
}
114116

115-
function getDelegationContext<TContext>({
117+
function getDelegationContext<TContext extends Record<string, any>>({
116118
request,
117119
schema,
118120
fieldName,
@@ -193,7 +195,9 @@ function validateRequest(delegationContext: DelegationContext<any>, document: Do
193195

194196
const GLOBAL_CONTEXT = {};
195197

196-
function getExecutor<TContext>(delegationContext: DelegationContext<TContext>): Executor<TContext> {
198+
function getExecutor<TContext extends Record<string, any>>(
199+
delegationContext: DelegationContext<TContext>
200+
): Executor<TContext> {
197201
const { subschemaConfig, targetSchema, context } = delegationContext;
198202

199203
let executor: Executor = subschemaConfig?.executor || createDefaultExecutor(targetSchema);

packages/delegate/src/resolveExternalValue.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { StitchingInfo, SubschemaConfig } from './types';
1919
import { annotateExternalObject, isExternalObject, mergeFields } from './mergeFields';
2020
import { Subschema } from './Subschema';
2121

22-
export function resolveExternalValue<TContext>(
22+
export function resolveExternalValue<TContext extends Record<string, any>>(
2323
result: any,
2424
unpathedErrors: Array<GraphQLError>,
2525
subschema: GraphQLSchema | SubschemaConfig<any, any, any, TContext>,
@@ -47,7 +47,7 @@ export function resolveExternalValue<TContext>(
4747
}
4848
}
4949

50-
function resolveExternalObject<TContext>(
50+
function resolveExternalObject<TContext extends Record<string, any>>(
5151
type: GraphQLCompositeType,
5252
object: any,
5353
unpathedErrors: Array<GraphQLError>,
@@ -91,7 +91,7 @@ function resolveExternalObject<TContext>(
9191
return mergeFields(mergedTypeInfo, object, subschema as Subschema, context, info);
9292
}
9393

94-
function resolveExternalList<TContext>(
94+
function resolveExternalList<TContext extends Record<string, any>>(
9595
type: GraphQLList<any>,
9696
list: Array<any>,
9797
unpathedErrors: Array<GraphQLError>,
@@ -113,7 +113,7 @@ function resolveExternalList<TContext>(
113113
);
114114
}
115115

116-
function resolveExternalListMember<TContext>(
116+
function resolveExternalListMember<TContext extends Record<string, any>>(
117117
type: GraphQLType,
118118
listMember: any,
119119
unpathedErrors: Array<GraphQLError>,

packages/links/src/linkToExecutor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
} from '@graphql-tools/utils';
1212

1313
export function linkToExecutor(link: ApolloLink): Executor {
14-
return function executorFromLink<TReturn, TArgs, TContext>(request: ExecutionRequest<TArgs, TContext>) {
14+
return function executorFromLink<TReturn, TArgs extends Record<string, any>, TContext>(
15+
request: ExecutionRequest<TArgs, TContext>
16+
) {
1517
const observable = execute(link, {
1618
query: request.document,
1719
operationName: request.operationName,

packages/loaders/git/src/parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { parseGraphQLSDL, parseGraphQLJSON, Source } from '@graphql-tools/utils';
1+
import { parseGraphQLSDL, parseGraphQLJSON, Source, GraphQLParseOptions } from '@graphql-tools/utils';
22

33
/**
44
* @internal
55
*/
6-
export function parse<T>({
6+
export function parse<T extends GraphQLParseOptions>({
77
path,
88
pointer,
99
content,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fetch } from 'cross-undici-fetch';
22

33
export type AsyncFetchFn = typeof fetch;
4-
export const defaultAsyncFetch: AsyncFetchFn = async (input: RequestInfo, init?: RequestInit): Promise<Response> => {
4+
export const defaultAsyncFetch: AsyncFetchFn = async (input, init) => {
55
return fetch(input, init);
66
};

packages/loaders/url/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,11 @@ export class UrlLoader implements Loader<LoadFromUrlOptions> {
505505
webSocketImpl
506506
);
507507

508-
return <TReturn, TArgs>({ document, variables, operationName }: ExecutionRequest<TArgs>) => {
508+
return <TReturn, TArgs extends Record<string, any>>({
509+
document,
510+
variables,
511+
operationName,
512+
}: ExecutionRequest<TArgs>) => {
509513
return observableToAsyncIterable(
510514
subscriptionClient.request({
511515
query: document,

0 commit comments

Comments
 (0)