From 0d8d60fac9c1744f72ebf120bafd8477ddb9c450 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Thu, 7 Sep 2023 21:53:44 -0700 Subject: [PATCH] add another case for @skip and @include when generating prisma selections --- .changeset/chilled-suits-knock.md | 5 +++ packages/plugin-prisma/src/util/map-query.ts | 42 ++++++++++++-------- 2 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 .changeset/chilled-suits-knock.md diff --git a/.changeset/chilled-suits-knock.md b/.changeset/chilled-suits-knock.md new file mode 100644 index 000000000..a17f88b78 --- /dev/null +++ b/.changeset/chilled-suits-knock.md @@ -0,0 +1,5 @@ +--- +'@pothos/plugin-prisma': patch +--- + +add another case for @skip and @include when generating prisma selections diff --git a/packages/plugin-prisma/src/util/map-query.ts b/packages/plugin-prisma/src/util/map-query.ts index 80e39c29e..b3bbb6c9f 100644 --- a/packages/plugin-prisma/src/util/map-query.ts +++ b/packages/plugin-prisma/src/util/map-query.ts @@ -63,8 +63,8 @@ function addTypeSelectionsForField( }; if ( - (pothosPrismaIndirectInclude?.path && pothosPrismaIndirectInclude.path.length > 0) || - (pothosPrismaIndirectInclude?.paths && pothosPrismaIndirectInclude.paths.length === 0) + (!!pothosPrismaIndirectInclude?.path && pothosPrismaIndirectInclude.path.length > 0) || + (!!pothosPrismaIndirectInclude?.paths && pothosPrismaIndirectInclude.paths.length === 0) ) { resolveIndirectIncludePaths( type, @@ -97,7 +97,7 @@ function addTypeSelectionsForField( state.mode = 'include'; } - if (pothosPrismaInclude || pothosPrismaSelect) { + if (pothosPrismaInclude ?? pothosPrismaSelect) { mergeSelection(state, { select: pothosPrismaSelect ? { ...pothosPrismaSelect } : undefined, include: pothosPrismaInclude ? { ...pothosPrismaInclude } : undefined, @@ -148,7 +148,11 @@ function resolveIndirectInclude( for (const sel of selection.selectionSet.selections) { switch (sel.kind) { case Kind.FIELD: - if (sel.name.value === include.name && (isObjectType(type) || isInterfaceType(type))) { + if ( + !fieldSkipped(info, sel) && + sel.name.value === include.name && + (isObjectType(type) || isInterfaceType(type)) + ) { const returnType = getNamedType(type.getFields()[sel.name.value].type); resolveIndirectInclude( @@ -270,17 +274,7 @@ function addFieldSelection( selection: FieldNode, indirectPath: string[], ) { - if (selection.name.value.startsWith('__')) { - return; - } - - const skip = getDirectiveValues(GraphQLSkipDirective, selection, info.variableValues); - if (skip?.if === true) { - return; - } - - const include = getDirectiveValues(GraphQLIncludeDirective, selection, info.variableValues); - if (include?.if === false) { + if (selection.name.value.startsWith('__') || fieldSkipped(info, selection)) { return; } @@ -329,8 +323,8 @@ function addFieldSelection( } if ( - (normalizedIndirectInclude?.path && normalizedIndirectInclude.path.length > 0) || - (normalizedIndirectInclude?.paths && normalizedIndirectInclude.paths.length > 0) + (!!normalizedIndirectInclude?.path && normalizedIndirectInclude.path.length > 0) || + (!!normalizedIndirectInclude?.paths && normalizedIndirectInclude.paths.length > 0) ) { resolveIndirectIncludePaths( returnType, @@ -560,3 +554,17 @@ function normalizeInclude(path: string[], type: GraphQLNamedType): IndirectInclu path: normalized, }; } + +function fieldSkipped(info: GraphQLResolveInfo, selection: FieldNode) { + const skip = getDirectiveValues(GraphQLSkipDirective, selection, info.variableValues); + if (skip?.if === true) { + return true; + } + + const include = getDirectiveValues(GraphQLIncludeDirective, selection, info.variableValues); + if (include?.if === false) { + return true; + } + + return false; +}