Skip to content

Commit

Permalink
feat(incontext-sdk): autoSelectionSetWithDepth option
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Aug 10, 2023
1 parent a720179 commit d8da4b2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .changeset/rude-numbers-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-mesh/runtime': patch
'@graphql-mesh/types': patch
---

New `autoSelectionSetWithDepth` option in the incontext sdk to avoid users to write manual selection
sets if return types don't match
10 changes: 1 addition & 9 deletions examples/openapi-javascript-wiki/additional-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ export const resolvers: Resolvers = {
},
context,
info,
selectionSet: /* GraphQL */ `
{
... on pageview_project {
items {
views
}
}
}
`,
autoSelectionSetWithDepth: 2,
},
);

Expand Down
25 changes: 21 additions & 4 deletions packages/runtime/src/in-context-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DocumentNode,
FieldNode,
getNamedType,
GraphQLObjectType,
GraphQLResolveInfo,
Expand All @@ -8,6 +9,7 @@ import {
Kind,
OperationDefinitionNode,
OperationTypeNode,
print,
SelectionSetNode,
} from 'graphql';
import {
Expand All @@ -26,7 +28,7 @@ import {
StitchingInfo,
SubschemaConfig,
} from '@graphql-tools/delegate';
import { isDocumentNode, memoize1 } from '@graphql-tools/utils';
import { buildOperationNodeForField, isDocumentNode, memoize1 } from '@graphql-tools/utils';
import { WrapQuery } from '@graphql-tools/wrap';
import { MESH_API_CONTEXT_SYMBOL } from './constants.js';

Expand Down Expand Up @@ -110,6 +112,7 @@ export async function getInContextSDK(
key,
argsFromKeys,
valuesFromResults,
autoSelectionSetWithDepth,
}: {
root: any;
args: any;
Expand All @@ -119,6 +122,7 @@ export async function getInContextSDK(
key?: string;
argsFromKeys?: (keys: string[]) => any;
valuesFromResults?: (result: any, keys?: string[]) => any;
autoSelectionSetWithDepth?: number;
}) => {
inContextSdkLogger.debug(`Called with`, {
args,
Expand Down Expand Up @@ -147,9 +151,22 @@ export async function getInContextSDK(
}
if (selectionCount === 0) {
if (!selectionSet) {
throw new Error(
`You have to provide 'selectionSet' for context.${rawSource.name}.${rootType.name}.${fieldName}`,
);
if (autoSelectionSetWithDepth) {
const operationNode = buildOperationNodeForField({
schema: transformedSchema,
kind: operationType as OperationTypeNode,
depthLimit: autoSelectionSetWithDepth,
field: fieldName,
});
selectionSet = print(
(operationNode.selectionSet.selections[0] as FieldNode).selectionSet,
);
console.log({ selectionSet });
} else {
throw new Error(
`You have to provide 'selectionSet' for context.${rawSource.name}.${rootType.name}.${fieldName}`,
);
}
}
commonDelegateOptions.info = {
...info,
Expand Down
12 changes: 11 additions & 1 deletion packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,20 @@ export type InContextSdkMethodInfoParams = {
info: GraphQLResolveInfo;
};

export type InContextSdkMethodAutoSelectionSetParams = {
// Use this parameter if the selection set of the return type doesn't match
autoSelectionSetWithDepth: number;
info?: GraphQLResolveInfo;
};

export type InContextSdkMethodParams<TDefaultReturn, TArgs, TContext, TKey, TReturn> = {
root?: any;
context: TContext;
} & (InContextSdkMethodCustomSelectionSetParams | InContextSdkMethodInfoParams) &
} & (
| InContextSdkMethodCustomSelectionSetParams
| InContextSdkMethodInfoParams
| InContextSdkMethodAutoSelectionSetParams
) &
(
| InContextSdkMethodBatchingParams<TDefaultReturn, TArgs, TKey, TReturn>
| InContextSdkMethodRegularParams<TDefaultReturn, TArgs, TReturn>
Expand Down

0 comments on commit d8da4b2

Please sign in to comment.