Skip to content

Commit

Permalink
add another case for @Skip and @include when generating prisma select…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
hayes committed Sep 8, 2023
1 parent 1e2a0f7 commit 0d8d60f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-suits-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-prisma': patch
---

add another case for @skip and @include when generating prisma selections
42 changes: 25 additions & 17 deletions packages/plugin-prisma/src/util/map-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

1 comment on commit 0d8d60f

@vercel
Copy link

@vercel vercel bot commented on 0d8d60f Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.