Skip to content

Commit

Permalink
Merge pull request #1017 from julioxavierr/prisma-support-client-dire…
Browse files Browse the repository at this point in the history
…ctives

Support Client Directives in Prisma plugin (@Skip and @include)
  • Loading branch information
hayes authored Sep 7, 2023
2 parents 9930fd6 + 1fc5b60 commit c3f242b
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-cups-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pothos/plugin-prisma": patch
---

Support Client Directives in Prisma plugin (@skip and @include)
19 changes: 16 additions & 3 deletions packages/plugin-prisma/src/util/map-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import {
FieldNode,
FragmentDefinitionNode,
getArgumentValues,
getDirectiveValues,
getNamedType,
GraphQLField,
GraphQLIncludeDirective,
GraphQLInterfaceType,
GraphQLNamedType,
GraphQLObjectType,
GraphQLResolveInfo,
GraphQLSkipDirective,
InlineFragmentNode,
isInterfaceType,
isObjectType,
Expand Down Expand Up @@ -198,7 +201,7 @@ function resolveIndirectInclude(
}

function addNestedSelections(
type: GraphQLObjectType | GraphQLInterfaceType,
type: GraphQLInterfaceType | GraphQLObjectType,
context: object,
info: GraphQLResolveInfo,
state: SelectionState,
Expand Down Expand Up @@ -260,7 +263,7 @@ function addNestedSelections(
}

function addFieldSelection(
type: GraphQLObjectType | GraphQLInterfaceType,
type: GraphQLInterfaceType | GraphQLObjectType,
context: object,
info: GraphQLResolveInfo,
state: SelectionState,
Expand All @@ -271,6 +274,16 @@ function addFieldSelection(
return;
}

const skip = getDirectiveValues(GraphQLSkipDirective, selection, info.variableValues);
if (skip?.if === true) {
return;
}

const include = getDirectiveValues(GraphQLIncludeDirective, selection, info.variableValues);
if (include?.if === false) {
return;
}

const field = type.getFields()[selection.name.value];

if (!field) {
Expand Down Expand Up @@ -397,7 +410,7 @@ export function queryFromInfo<T extends SelectionMap['select'] | undefined = und
path?: string[];
paths?: string[][];
withUsageCheck?: boolean;
}): { select: T } | { include?: {} } {
}): { include?: {} } | { select: T } {
const returnType = getNamedType(info.returnType);
const type = typeName ? info.schema.getTypeMap()[typeName] : returnType;

Expand Down
122 changes: 122 additions & 0 deletions packages/plugin-prisma/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,128 @@ describe('prisma', () => {
`);
});

it('skips fields based on @skip and @include directives', async () => {
const query = gql`
query {
me {
id
profile @skip(if: true) {
bio
}
posts(limit: 1) @include(if: false) {
id
}
}
}
`;

const result = await execute({
schema,
document: query,
contextValue: { user: { id: 1 } },
});

expect(result).toMatchInlineSnapshot(`
{
"data": {
"me": {
"id": "VXNlcjox",
},
},
}
`);

expect(queries).toMatchInlineSnapshot(`
[
{
"action": "findUnique",
"args": {
"where": {
"id": 1,
},
},
"dataPath": [],
"model": "User",
"runInTransaction": false,
},
]
`);
});

it('includes fields based on @skip and @include directives', async () => {
const query = gql`
query {
me {
id
profile @skip(if: false) {
bio
}
posts(limit: 1) @include(if: true) {
id
}
}
}
`;

const result = await execute({
schema,
document: query,
contextValue: { user: { id: 1 } },
});

expect(result).toMatchInlineSnapshot(`
{
"data": {
"me": {
"id": "VXNlcjox",
"posts": [
{
"id": "250",
},
],
"profile": {
"bio": "Debitis perspiciatis unde sunt.",
},
},
},
}
`);

expect(queries).toMatchInlineSnapshot(`
[
{
"action": "findUnique",
"args": {
"include": {
"posts": {
"include": {
"comments": {
"include": {
"author": true,
},
"take": 3,
},
},
"orderBy": {
"createdAt": "desc",
},
"take": 1,
"where": undefined,
},
"profile": true,
},
"where": {
"id": 1,
},
},
"dataPath": [],
"model": "User",
"runInTransaction": false,
},
]
`);
});

it('queries decimals', async () => {
const query = gql`
query {
Expand Down

0 comments on commit c3f242b

Please sign in to comment.