Skip to content

Commit

Permalink
refactor(endpoint): no longer necessary
Browse files Browse the repository at this point in the history
BREAKING CHANGE: batching is now performed (when enabled) for all subschema config objects with identical executors, removing the need for the endpoint abstraction
  • Loading branch information
yaacovCR committed Oct 23, 2020
1 parent 134827d commit 69ee3cd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 47 deletions.
7 changes: 2 additions & 5 deletions packages/delegate/src/Subschema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
CreateProxyingResolverFn,
Subscriber,
Executor,
Endpoint,
EndpointBatchingOptions,
BatchingOptions,
} from './types';

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

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

this.createProxyingResolver = config.createProxyingResolver;
this.transforms = config.transforms ?? [];
Expand Down
14 changes: 7 additions & 7 deletions packages/delegate/src/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function delegateRequest({
targetFieldName = fieldName;
}

const { targetSchema, targetRootValue, subschemaConfig, endpoint, allTransforms } = collectTargetParameters(
const { targetSchema, targetRootValue, subschemaConfig, allTransforms } = collectTargetParameters(
subschemaOrSubschemaConfig,
rootValue,
info,
Expand Down Expand Up @@ -149,14 +149,15 @@ export function delegateRequest({

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

if (endpoint?.batch) {
if (subschemaConfig?.batch) {
const batchingOptions = subschemaConfig?.batchingOptions;
executor = getBatchingExecutor(
context,
executor,
endpoint?.batchingOptions?.dataLoaderOptions,
endpoint?.batchingOptions?.extensionsReducer
batchingOptions?.dataLoaderOptions,
batchingOptions?.extensionsReducer
);
}

Expand All @@ -175,7 +176,7 @@ export function delegateRequest({
}

const subscriber =
endpoint?.subscriber || createDefaultSubscriber(targetSchema, subschemaConfig?.rootValue || targetRootValue);
subschemaConfig?.subscriber || createDefaultSubscriber(targetSchema, subschemaConfig?.rootValue || targetRootValue);

return subscriber({
...processedRequest,
Expand Down Expand Up @@ -218,7 +219,6 @@ function collectTargetParameters(
targetSchema: subschemaOrSubschemaConfig.schema,
targetRootValue: rootValue ?? subschemaOrSubschemaConfig?.rootValue ?? info?.rootValue ?? emptyObject,
subschemaConfig: subschemaOrSubschemaConfig,
endpoint: subschemaOrSubschemaConfig.endpoint ?? subschemaOrSubschemaConfig,
allTransforms:
subschemaOrSubschemaConfig.transforms != null
? subschemaOrSubschemaConfig.transforms.concat(transforms)
Expand Down
23 changes: 8 additions & 15 deletions packages/delegate/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,21 @@ export interface ICreateProxyingResolverOptions {

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

export interface Endpoint<K = any, V = any, C = K> {
rootValue?: Record<string, any>;
executor?: Executor;
subscriber?: Subscriber;
batch?: boolean;
batchingOptions?: EndpointBatchingOptions<K, V, C>;
}

export interface EndpointBatchingOptions<K = any, V = any, C = K> {
export interface BatchingOptions<K = any, V = any, C = K> {
extensionsReducer?: (mergedExtensions: Record<string, any>, executionParams: ExecutionParams) => Record<string, any>;
dataLoaderOptions?: DataLoader.Options<K, V, C>;
}

export interface SubschemaPermutation {
export interface SubschemaConfig<K = any, V = any, C = K> {
schema: GraphQLSchema;
createProxyingResolver?: CreateProxyingResolverFn;
transforms?: Array<Transform>;
merge?: Record<string, MergedTypeConfig>;
}

export interface SubschemaConfig<K = any, V = any, C = K> extends SubschemaPermutation, Endpoint<K, V, C> {
schema: GraphQLSchema;
endpoint?: Endpoint;
rootValue?: Record<string, any>;
executor?: Executor;
subscriber?: Subscriber;
batch?: boolean;
batchingOptions?: BatchingOptions<K, V, C>;
}

export interface MergedTypeConfig<K = any, V = any> {
Expand Down
21 changes: 10 additions & 11 deletions packages/delegate/tests/batchExecution.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { graphql, execute, ExecutionResult } from 'graphql';

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

Expand Down Expand Up @@ -61,7 +61,7 @@ describe('batch execution', () => {
expect(executions).toEqual(1);
});

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

let executions = 0;

const endpoint: Endpoint = {
batch: true,
executor: ((params: ExecutionParams): ExecutionResult => {
executions++;
return execute(innerSchemaA, params.document, undefined, params.context, params.variables) as ExecutionResult;
}) as SyncExecutor
};
const executor = ((params: ExecutionParams): ExecutionResult => {
executions++;
return execute(innerSchemaA, params.document, undefined, params.context, params.variables) as ExecutionResult;
}) as Executor;

const innerSubschemaConfigA: Array<SubschemaConfig> = [{
schema: innerSchemaA,
Expand All @@ -121,7 +118,8 @@ describe('batch execution', () => {
args: () => ({}),
},
},
endpoint,
batch: true,
executor,
}, {
schema: innerSchemaA,
transforms: [new FilterObjectFields((typeName, fieldName) => typeName !== 'Object' || fieldName !== 'field1')],
Expand All @@ -131,7 +129,8 @@ describe('batch execution', () => {
args: () => ({}),
},
},
endpoint,
batch: true,
executor,
}];

const innerSubschemaConfigB: SubschemaConfig = {
Expand Down
11 changes: 2 additions & 9 deletions packages/stitch/src/isolateComputedFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,9 @@ export function isolateComputedFields(subschemaConfig: SubschemaConfig): Array<S
});

if (Object.keys(isolatedSchemaTypes).length) {
const endpoint = subschemaConfig.endpoint ?? {
rootValue: subschemaConfig.rootValue,
executor: subschemaConfig.executor,
subscriber: subschemaConfig.subscriber,
batch: subschemaConfig.batch,
batchingOptions: subschemaConfig.batchingOptions,
};
return [
filterBaseSubschema({ ...subschemaConfig, endpoint, merge: baseSchemaTypes }, isolatedSchemaTypes),
filterIsolatedSubschema({ ...subschemaConfig, endpoint, merge: isolatedSchemaTypes }),
filterBaseSubschema({ ...subschemaConfig, merge: baseSchemaTypes }, isolatedSchemaTypes),
filterIsolatedSubschema({ ...subschemaConfig, merge: isolatedSchemaTypes }),
];
}

Expand Down

0 comments on commit 69ee3cd

Please sign in to comment.