From 0515ad5c0dd3faabe36546028e3cdce801742e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Xavier?= Date: Thu, 7 Sep 2023 17:00:54 -0300 Subject: [PATCH 1/4] [prisma] support @skip and @include client directives --- packages/plugin-prisma/src/util/map-query.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/plugin-prisma/src/util/map-query.ts b/packages/plugin-prisma/src/util/map-query.ts index 3787c98a1..80e39c29e 100644 --- a/packages/plugin-prisma/src/util/map-query.ts +++ b/packages/plugin-prisma/src/util/map-query.ts @@ -4,12 +4,15 @@ import { FieldNode, FragmentDefinitionNode, getArgumentValues, + getDirectiveValues, getNamedType, GraphQLField, + GraphQLIncludeDirective, GraphQLInterfaceType, GraphQLNamedType, GraphQLObjectType, GraphQLResolveInfo, + GraphQLSkipDirective, InlineFragmentNode, isInterfaceType, isObjectType, @@ -198,7 +201,7 @@ function resolveIndirectInclude( } function addNestedSelections( - type: GraphQLObjectType | GraphQLInterfaceType, + type: GraphQLInterfaceType | GraphQLObjectType, context: object, info: GraphQLResolveInfo, state: SelectionState, @@ -260,7 +263,7 @@ function addNestedSelections( } function addFieldSelection( - type: GraphQLObjectType | GraphQLInterfaceType, + type: GraphQLInterfaceType | GraphQLObjectType, context: object, info: GraphQLResolveInfo, state: SelectionState, @@ -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) { @@ -397,7 +410,7 @@ export function queryFromInfo Date: Thu, 7 Sep 2023 17:29:18 -0300 Subject: [PATCH 2/4] [prisma] test query with skip and include directives --- packages/plugin-prisma/tests/index.test.ts | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/packages/plugin-prisma/tests/index.test.ts b/packages/plugin-prisma/tests/index.test.ts index ec84b5774..90c7e311b 100644 --- a/packages/plugin-prisma/tests/index.test.ts +++ b/packages/plugin-prisma/tests/index.test.ts @@ -65,6 +65,54 @@ 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('queries decimals', async () => { const query = gql` query { From 601a99f0d0913502a9e9bee5dc0afdbe98daf8ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Xavier?= Date: Thu, 7 Sep 2023 17:45:33 -0300 Subject: [PATCH 3/4] [prisma] test query with inverted skip and include directives --- packages/plugin-prisma/tests/index.test.ts | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/packages/plugin-prisma/tests/index.test.ts b/packages/plugin-prisma/tests/index.test.ts index 90c7e311b..2ed62f8cc 100644 --- a/packages/plugin-prisma/tests/index.test.ts +++ b/packages/plugin-prisma/tests/index.test.ts @@ -113,6 +113,80 @@ describe('prisma', () => { `); }); + 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 { From 1fc5b60bebe7eaa0d966255e0f6f670a49d88f49 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Thu, 7 Sep 2023 14:09:37 -0700 Subject: [PATCH 4/4] Create twenty-cups-marry.md --- .changeset/twenty-cups-marry.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/twenty-cups-marry.md diff --git a/.changeset/twenty-cups-marry.md b/.changeset/twenty-cups-marry.md new file mode 100644 index 000000000..8707a0401 --- /dev/null +++ b/.changeset/twenty-cups-marry.md @@ -0,0 +1,5 @@ +--- +"@pothos/plugin-prisma": patch +--- + +Support Client Directives in Prisma plugin (@skip and @include)