Skip to content

Commit 69ee3cd

Browse files
committed
refactor(endpoint): no longer necessary
BREAKING CHANGE: batching is now performed (when enabled) for all subschema config objects with identical executors, removing the need for the endpoint abstraction
1 parent 134827d commit 69ee3cd

File tree

5 files changed

+29
-47
lines changed

5 files changed

+29
-47
lines changed

packages/delegate/src/Subschema.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
CreateProxyingResolverFn,
88
Subscriber,
99
Executor,
10-
Endpoint,
11-
EndpointBatchingOptions,
10+
BatchingOptions,
1211
} from './types';
1312

1413
import { applySchemaTransforms } from './applySchemaTransforms';
@@ -28,8 +27,7 @@ export class Subschema<K = any, V = any, C = K> implements ISubschema {
2827
public executor?: Executor;
2928
public subscriber?: Subscriber;
3029
public batch?: boolean;
31-
public batchingOptions?: EndpointBatchingOptions<K, V, C>;
32-
public endpoint?: Endpoint;
30+
public batchingOptions?: BatchingOptions<K, V, C>;
3331

3432
public createProxyingResolver?: CreateProxyingResolverFn;
3533
public transforms: Array<Transform>;
@@ -45,7 +43,6 @@ export class Subschema<K = any, V = any, C = K> implements ISubschema {
4543
this.subscriber = config.subscriber;
4644
this.batch = config.batch;
4745
this.batchingOptions = config.batchingOptions;
48-
this.endpoint = config.endpoint;
4946

5047
this.createProxyingResolver = config.createProxyingResolver;
5148
this.transforms = config.transforms ?? [];

packages/delegate/src/delegateToSchema.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function delegateRequest({
117117
targetFieldName = fieldName;
118118
}
119119

120-
const { targetSchema, targetRootValue, subschemaConfig, endpoint, allTransforms } = collectTargetParameters(
120+
const { targetSchema, targetRootValue, subschemaConfig, allTransforms } = collectTargetParameters(
121121
subschemaOrSubschemaConfig,
122122
rootValue,
123123
info,
@@ -149,14 +149,15 @@ export function delegateRequest({
149149

150150
if (targetOperation === 'query' || targetOperation === 'mutation') {
151151
let executor: Executor =
152-
endpoint?.executor || createDefaultExecutor(targetSchema, subschemaConfig?.rootValue || targetRootValue);
152+
subschemaConfig?.executor || createDefaultExecutor(targetSchema, subschemaConfig?.rootValue || targetRootValue);
153153

154-
if (endpoint?.batch) {
154+
if (subschemaConfig?.batch) {
155+
const batchingOptions = subschemaConfig?.batchingOptions;
155156
executor = getBatchingExecutor(
156157
context,
157158
executor,
158-
endpoint?.batchingOptions?.dataLoaderOptions,
159-
endpoint?.batchingOptions?.extensionsReducer
159+
batchingOptions?.dataLoaderOptions,
160+
batchingOptions?.extensionsReducer
160161
);
161162
}
162163

@@ -175,7 +176,7 @@ export function delegateRequest({
175176
}
176177

177178
const subscriber =
178-
endpoint?.subscriber || createDefaultSubscriber(targetSchema, subschemaConfig?.rootValue || targetRootValue);
179+
subschemaConfig?.subscriber || createDefaultSubscriber(targetSchema, subschemaConfig?.rootValue || targetRootValue);
179180

180181
return subscriber({
181182
...processedRequest,
@@ -218,7 +219,6 @@ function collectTargetParameters(
218219
targetSchema: subschemaOrSubschemaConfig.schema,
219220
targetRootValue: rootValue ?? subschemaOrSubschemaConfig?.rootValue ?? info?.rootValue ?? emptyObject,
220221
subschemaConfig: subschemaOrSubschemaConfig,
221-
endpoint: subschemaOrSubschemaConfig.endpoint ?? subschemaOrSubschemaConfig,
222222
allTransforms:
223223
subschemaOrSubschemaConfig.transforms != null
224224
? subschemaOrSubschemaConfig.transforms.concat(transforms)

packages/delegate/src/types.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,28 +150,21 @@ export interface ICreateProxyingResolverOptions {
150150

151151
export type CreateProxyingResolverFn = (options: ICreateProxyingResolverOptions) => GraphQLFieldResolver<any, any>;
152152

153-
export interface Endpoint<K = any, V = any, C = K> {
154-
rootValue?: Record<string, any>;
155-
executor?: Executor;
156-
subscriber?: Subscriber;
157-
batch?: boolean;
158-
batchingOptions?: EndpointBatchingOptions<K, V, C>;
159-
}
160-
161-
export interface EndpointBatchingOptions<K = any, V = any, C = K> {
153+
export interface BatchingOptions<K = any, V = any, C = K> {
162154
extensionsReducer?: (mergedExtensions: Record<string, any>, executionParams: ExecutionParams) => Record<string, any>;
163155
dataLoaderOptions?: DataLoader.Options<K, V, C>;
164156
}
165157

166-
export interface SubschemaPermutation {
158+
export interface SubschemaConfig<K = any, V = any, C = K> {
159+
schema: GraphQLSchema;
167160
createProxyingResolver?: CreateProxyingResolverFn;
168161
transforms?: Array<Transform>;
169162
merge?: Record<string, MergedTypeConfig>;
170-
}
171-
172-
export interface SubschemaConfig<K = any, V = any, C = K> extends SubschemaPermutation, Endpoint<K, V, C> {
173-
schema: GraphQLSchema;
174-
endpoint?: Endpoint;
163+
rootValue?: Record<string, any>;
164+
executor?: Executor;
165+
subscriber?: Subscriber;
166+
batch?: boolean;
167+
batchingOptions?: BatchingOptions<K, V, C>;
175168
}
176169

177170
export interface MergedTypeConfig<K = any, V = any> {

packages/delegate/tests/batchExecution.test.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { graphql, execute, ExecutionResult } from 'graphql';
22

33
import { makeExecutableSchema } from '@graphql-tools/schema';
4-
import { delegateToSchema, SubschemaConfig, ExecutionParams, SyncExecutor, Endpoint } from '../src';
4+
import { delegateToSchema, SubschemaConfig, ExecutionParams, SyncExecutor, Executor } from '../src';
55
import { stitchSchemas } from '@graphql-tools/stitch';
66
import { FilterObjectFields } from '@graphql-tools/wrap';
77

@@ -61,7 +61,7 @@ describe('batch execution', () => {
6161
expect(executions).toEqual(1);
6262
});
6363

64-
it('should share batching dataloader between subschemas when using a common endpoint', async () => {
64+
it('should share batching dataloader between subschemas when using a common executor', async () => {
6565
const innerSchemaA = makeExecutableSchema({
6666
typeDefs: `
6767
type Object {
@@ -104,13 +104,10 @@ describe('batch execution', () => {
104104

105105
let executions = 0;
106106

107-
const endpoint: Endpoint = {
108-
batch: true,
109-
executor: ((params: ExecutionParams): ExecutionResult => {
110-
executions++;
111-
return execute(innerSchemaA, params.document, undefined, params.context, params.variables) as ExecutionResult;
112-
}) as SyncExecutor
113-
};
107+
const executor = ((params: ExecutionParams): ExecutionResult => {
108+
executions++;
109+
return execute(innerSchemaA, params.document, undefined, params.context, params.variables) as ExecutionResult;
110+
}) as Executor;
114111

115112
const innerSubschemaConfigA: Array<SubschemaConfig> = [{
116113
schema: innerSchemaA,
@@ -121,7 +118,8 @@ describe('batch execution', () => {
121118
args: () => ({}),
122119
},
123120
},
124-
endpoint,
121+
batch: true,
122+
executor,
125123
}, {
126124
schema: innerSchemaA,
127125
transforms: [new FilterObjectFields((typeName, fieldName) => typeName !== 'Object' || fieldName !== 'field1')],
@@ -131,7 +129,8 @@ describe('batch execution', () => {
131129
args: () => ({}),
132130
},
133131
},
134-
endpoint,
132+
batch: true,
133+
executor,
135134
}];
136135

137136
const innerSubschemaConfigB: SubschemaConfig = {

packages/stitch/src/isolateComputedFields.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,9 @@ export function isolateComputedFields(subschemaConfig: SubschemaConfig): Array<S
4646
});
4747

4848
if (Object.keys(isolatedSchemaTypes).length) {
49-
const endpoint = subschemaConfig.endpoint ?? {
50-
rootValue: subschemaConfig.rootValue,
51-
executor: subschemaConfig.executor,
52-
subscriber: subschemaConfig.subscriber,
53-
batch: subschemaConfig.batch,
54-
batchingOptions: subschemaConfig.batchingOptions,
55-
};
5649
return [
57-
filterBaseSubschema({ ...subschemaConfig, endpoint, merge: baseSchemaTypes }, isolatedSchemaTypes),
58-
filterIsolatedSubschema({ ...subschemaConfig, endpoint, merge: isolatedSchemaTypes }),
50+
filterBaseSubschema({ ...subschemaConfig, merge: baseSchemaTypes }, isolatedSchemaTypes),
51+
filterIsolatedSubschema({ ...subschemaConfig, merge: isolatedSchemaTypes }),
5952
];
6053
}
6154

0 commit comments

Comments
 (0)