Skip to content

Commit

Permalink
enhance(runtime): use in-context-sdk for wrapping resolvers to have b…
Browse files Browse the repository at this point in the history
…etter tracing
  • Loading branch information
ardatan committed Sep 6, 2022
1 parent a6690c6 commit 44b8681
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/tall-dingos-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-mesh/runtime': minor
'@graphql-mesh/types': minor
---

Use In Context SDK for wrapping resolvers for better tracing
12 changes: 11 additions & 1 deletion packages/runtime/src/get-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getHeadersObj,
} from '@graphql-mesh/utils';

import { SubschemaConfig } from '@graphql-tools/delegate';
import { CreateProxyingResolverFn, SubschemaConfig } from '@graphql-tools/delegate';
import { AggregateError, ExecutionResult, isAsyncIterable, mapAsyncIterator, memoize1 } from '@graphql-tools/utils';
import { enableIf, envelop, PluginOrDisabledPlugin, useExtendContext } from '@envelop/core';
import { OneOfInputObjectsRule, useExtendedValidation } from '@envelop/extended-validation';
Expand Down Expand Up @@ -92,6 +92,15 @@ export function wrapFetchWithPlugins(plugins: MeshPlugin<any>[]): MeshFetch {
};
}

// Use in-context-sdk for tracing
function createProxyingResolverFactory(apiName: string): CreateProxyingResolverFn {
return function createProxyingResolver() {
return function proxyingResolver(root, args, context, info) {
return context[apiName][info.parentType.name][info.fieldName]({ root, args, context, info });
};
};
}

export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
const rawSources: RawSourceOutput[] = [];
const {
Expand Down Expand Up @@ -185,6 +194,7 @@ export async function getMesh(options: GetMeshOptions): Promise<MeshInstance> {
handler: apiSource.handler,
batch: 'batch' in source ? source.batch : true,
merge: apiSource.merge,
createProxyingResolver: createProxyingResolverFactory(apiName),
});
} catch (e: any) {
sourceLogger.error(`Failed to generate the schema`, e);
Expand Down
9 changes: 8 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import { IResolvers, Executor } from '@graphql-tools/utils';
import { GraphQLSchema, GraphQLResolveInfo, DocumentNode, SelectionSetNode } from 'graphql';
import * as YamlConfig from './config';
import { Transform, MergedTypeConfig, SubschemaConfig, IDelegateToSchemaOptions } from '@graphql-tools/delegate';
import {
Transform,
MergedTypeConfig,
SubschemaConfig,
IDelegateToSchemaOptions,
CreateProxyingResolverFn,
} from '@graphql-tools/delegate';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import { MeshStore } from '@graphql-mesh/store';
import configSchema from './config-schema.json';
Expand Down Expand Up @@ -186,6 +192,7 @@ export type RawSourceOutput = {
handler: MeshHandler;
batch: boolean;
merge?: Record<string, MergedTypeConfig>;
createProxyingResolver: CreateProxyingResolverFn<any>;
};

export type GraphQLOperation<TData, TVariables> = TypedDocumentNode<TData, TVariables> | string;
Expand Down

0 comments on commit 44b8681

Please sign in to comment.