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

typescript@4.7 readiness (refine generic typings via extends) #4382

Merged
merged 5 commits into from
Apr 13, 2022
Merged
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
21 changes: 21 additions & 0 deletions .changeset/slow-singers-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@graphql-tools/delegate': patch
'@graphql-tools/links': patch
'@graphql-tools/git-loader': patch
'@graphql-tools/url-loader': patch
'@graphql-tools/stitch': patch
'@graphql-tools/utils': patch
'@graphql-tools/wrap': patch
---

Refine generic typings using `extends X` when appropriate

Typescript 4.7 has stricter requirements around generics
which is explained well in the related PR:
https://github.com/microsoft/TypeScript/pull/48366

These changes resolve the errors that these packages will
face when attempting to upgrade to TS 4.7 (still in beta
at the time of writing this). Landing these changes now
will allow other TS libraries which depend on these
packages to experiment with TS 4.7 in the meantime.
4 changes: 2 additions & 2 deletions packages/delegate/src/Transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Transformation<TContext> {
context: Record<string, any>;
}

export class Transformer<TContext = Record<string, any>> {
export class Transformer<TContext extends Record<string, any> = Record<string, any>> {
private transformations: Array<Transformation<TContext>> = [];
private delegationContext: DelegationContext<TContext>;

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

// from rigth to left
// from right to left
for (let i = this.transformations.length - 1; i >= 0; i--) {
const transformation = this.transformations[i];
if (transformation.transform.transformResult) {
Expand Down
2 changes: 1 addition & 1 deletion packages/delegate/src/checkResultAndHandleErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AggregateError, getResponseKeyFromInfo, ExecutionResult, relocatedError
import { DelegationContext } from './types';
import { resolveExternalValue } from './resolveExternalValue';

export function checkResultAndHandleErrors<TContext>(
export function checkResultAndHandleErrors<TContext extends Record<string, any>>(
result: ExecutionResult,
delegationContext: DelegationContext<TContext>
): any {
Expand Down
20 changes: 12 additions & 8 deletions packages/delegate/src/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ import { Subschema } from './Subschema';
import { createRequest, getDelegatingOperation } from './createRequest';
import { Transformer } from './Transformer';

export function delegateToSchema<TContext = Record<string, any>, TArgs = any>(
options: IDelegateToSchemaOptions<TContext, TArgs>
): any {
export function delegateToSchema<
TContext extends Record<string, any> = Record<string, any>,
TArgs extends Record<string, any> = any
>(options: IDelegateToSchemaOptions<TContext, TArgs>): any {
const {
info,
schema,
Expand Down Expand Up @@ -85,9 +86,10 @@ function getDelegationReturnType(
return rootType.getFields()[fieldName].type;
}

export function delegateRequest<TContext = Record<string, any>, TArgs = any>(
options: IDelegateRequestOptions<TContext, TArgs>
) {
export function delegateRequest<
TContext extends Record<string, any> = Record<string, any>,
TArgs extends Record<string, any> = any
>(options: IDelegateRequestOptions<TContext, TArgs>) {
const delegationContext = getDelegationContext(options);

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

function getDelegationContext<TContext>({
function getDelegationContext<TContext extends Record<string, any>>({
request,
schema,
fieldName,
Expand Down Expand Up @@ -193,7 +195,9 @@ function validateRequest(delegationContext: DelegationContext<any>, document: Do

const GLOBAL_CONTEXT = {};

function getExecutor<TContext>(delegationContext: DelegationContext<TContext>): Executor<TContext> {
function getExecutor<TContext extends Record<string, any>>(
delegationContext: DelegationContext<TContext>
): Executor<TContext> {
const { subschemaConfig, targetSchema, context } = delegationContext;

let executor: Executor = subschemaConfig?.executor || createDefaultExecutor(targetSchema);
Expand Down
8 changes: 4 additions & 4 deletions packages/delegate/src/resolveExternalValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { StitchingInfo, SubschemaConfig } from './types';
import { annotateExternalObject, isExternalObject, mergeFields } from './mergeFields';
import { Subschema } from './Subschema';

export function resolveExternalValue<TContext>(
export function resolveExternalValue<TContext extends Record<string, any>>(
result: any,
unpathedErrors: Array<GraphQLError>,
subschema: GraphQLSchema | SubschemaConfig<any, any, any, TContext>,
Expand Down Expand Up @@ -47,7 +47,7 @@ export function resolveExternalValue<TContext>(
}
}

function resolveExternalObject<TContext>(
function resolveExternalObject<TContext extends Record<string, any>>(
type: GraphQLCompositeType,
object: any,
unpathedErrors: Array<GraphQLError>,
Expand Down Expand Up @@ -91,7 +91,7 @@ function resolveExternalObject<TContext>(
return mergeFields(mergedTypeInfo, object, subschema as Subschema, context, info);
}

function resolveExternalList<TContext>(
function resolveExternalList<TContext extends Record<string, any>>(
type: GraphQLList<any>,
list: Array<any>,
unpathedErrors: Array<GraphQLError>,
Expand All @@ -113,7 +113,7 @@ function resolveExternalList<TContext>(
);
}

function resolveExternalListMember<TContext>(
function resolveExternalListMember<TContext extends Record<string, any>>(
type: GraphQLType,
listMember: any,
unpathedErrors: Array<GraphQLError>,
Expand Down
4 changes: 3 additions & 1 deletion packages/links/src/linkToExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
} from '@graphql-tools/utils';

export function linkToExecutor(link: ApolloLink): Executor {
return function executorFromLink<TReturn, TArgs, TContext>(request: ExecutionRequest<TArgs, TContext>) {
return function executorFromLink<TReturn, TArgs extends Record<string, any>, TContext>(
request: ExecutionRequest<TArgs, TContext>
) {
const observable = execute(link, {
query: request.document,
operationName: request.operationName,
Expand Down
4 changes: 2 additions & 2 deletions packages/loaders/git/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { parseGraphQLSDL, parseGraphQLJSON, Source } from '@graphql-tools/utils';
import { parseGraphQLSDL, parseGraphQLJSON, Source, GraphQLParseOptions } from '@graphql-tools/utils';

/**
* @internal
*/
export function parse<T>({
export function parse<T extends GraphQLParseOptions>({
path,
pointer,
content,
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/url/src/defaultAsyncFetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fetch } from 'cross-undici-fetch';

export type AsyncFetchFn = typeof fetch;
export const defaultAsyncFetch: AsyncFetchFn = async (input: RequestInfo, init?: RequestInit): Promise<Response> => {
export const defaultAsyncFetch: AsyncFetchFn = async (input, init) => {
return fetch(input, init);
};
6 changes: 5 additions & 1 deletion packages/loaders/url/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,11 @@ export class UrlLoader implements Loader<LoadFromUrlOptions> {
webSocketImpl
);

return <TReturn, TArgs>({ document, variables, operationName }: ExecutionRequest<TArgs>) => {
return <TReturn, TArgs extends Record<string, any>>({
document,
variables,
operationName,
}: ExecutionRequest<TArgs>) => {
return observableToAsyncIterable(
subscriptionClient.request({
query: document,
Expand Down
Loading