Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Client Directives in Prisma plugin (@skip and @include) #1017

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 48 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,54 @@ describe('prisma', () => {
`);
});

it('skips fields based on @skip and @include directives', async () => {
Copy link
Owner

Choose a reason for hiding this comment

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

sorry to keep asking for more changes one at a time, but would also be nice to check the inverted condition, so that if the conditions is reversed the selection includes the expected relations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no worries! Just pushed one more commit

Copy link
Owner

Choose a reason for hiding this comment

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

thanks!

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 {
Expand Down