Skip to content

feat(delegate): pass info to fetcher/link #1397

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

Merged
merged 1 commit into from
Apr 23, 2020
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
16 changes: 11 additions & 5 deletions src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,23 @@ declare module 'graphql' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IGraphQLToolsResolveInfo extends GraphQLResolveInfo {}

export type Fetcher = (
operation: IFetcherOperation,
) => Promise<ExecutionResult>;
export type Fetcher = (options: IFetcherOptions) => Promise<ExecutionResult>;

export interface IFetcherOperation {
export interface IFetcherOptions {
query: DocumentNode;
operationName?: string;
variables?: Record<string, any>;
context?: Record<string, any>;
context?: {
graphqlContext?: Record<string, any>;
graphqlResolveInfo?: GraphQLResolveInfo;
[key: string]: any;
};
}

// for backwards compatibility
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IFetcherOperation extends IFetcherOptions {}

export type Dispatcher = (context: any) => ApolloLink | Fetcher;

export interface SubschemaConfig {
Expand Down
35 changes: 23 additions & 12 deletions src/delegate/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ export function delegateRequest({

const executionResult = executor({
document: processedRequest.document,
context,
variables: processedRequest.variables,
context,
info,
});

if (executionResult instanceof Promise) {
Expand All @@ -225,8 +226,9 @@ export function delegateRequest({

return subscriber({
document: processedRequest.document,
context,
variables: processedRequest.variables,
context,
info,
}).then(
(
subscriptionResult:
Expand Down Expand Up @@ -263,12 +265,14 @@ function createExecutor(
subschemaConfig?: SubschemaConfig,
): ({
document,
context,
variables,
context,
info,
}: {
document: DocumentNode;
context?: Record<string, any>;
variables?: Record<string, any>;
variables: Record<string, any>;
context: Record<string, any>;
info: GraphQLResolveInfo;
}) => Promise<ExecutionResult> | ExecutionResult {
let fetcher: Fetcher;
let targetRootValue: Record<string, any> = rootValue;
Expand All @@ -291,11 +295,16 @@ function createExecutor(
}

if (fetcher != null) {
return ({ document, context: graphqlContext, variables }) =>
return ({
document: query,
variables,
context: graphqlContext,
info: graphqlResolveInfo,
}) =>
fetcher({
query: document,
query,
variables,
context: { graphqlContext },
context: { graphqlContext, graphqlResolveInfo },
});
}

Expand All @@ -316,12 +325,14 @@ function createSubscriber(
subschemaConfig?: SubschemaConfig,
): ({
document,
context,
variables,
context,
info,
}: {
document: DocumentNode;
context?: Record<string, any>;
variables?: Record<string, any>;
variables: Record<string, any>;
context: Record<string, any>;
info: GraphQLResolveInfo;
}) => Promise<AsyncIterator<ExecutionResult> | ExecutionResult> {
let link: ApolloLink;
let targetRootValue: Record<string, any> = rootValue;
Expand Down Expand Up @@ -350,7 +361,7 @@ function createSubscriber(
};
}

return ({ document, context: graphqlContext, variables }) =>
return ({ document, variables, context: graphqlContext }) =>
subscribe({
schema,
document,
Expand Down
11 changes: 8 additions & 3 deletions src/stitch/linkToFetcher.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { ApolloLink, toPromise, execute, ExecutionResult } from 'apollo-link';

import { Fetcher, IFetcherOperation } from '../Interfaces';
import { Fetcher, IFetcherOptions } from '../Interfaces';

export { execute } from 'apollo-link';

export default function linkToFetcher(link: ApolloLink): Fetcher {
return (fetcherOperation: IFetcherOperation): Promise<ExecutionResult> =>
toPromise(execute(link, fetcherOperation));
return ({
query,
operationName,
variables,
context,
}: IFetcherOptions): Promise<ExecutionResult> =>
toPromise(execute(link, { query, operationName, variables, context }));
}