Computed fields with the Prisma- & Relay-Plugins #320
Answered
by
hayes
mkaradeniz
asked this question in
Q&A
-
Hi, I have computed field that gets data from a relation (like in the User builder.prismaNode('User', {
include: { groups: { select: { permissions: true } } },
fields: t => ({
permissions: t.stringList({
resolve: user => {
return uniq(user.groups.flatMap(group => group.permissions));
},
}),
groups: t.relatedConnection('groups', { cursor: 'id', totalCount: true }),
}),
findUnique: id => ({ id }),
id: {
resolve: user => {
return user.id;
},
},
}); UserGroup builder.prismaNode('UserGroup', {
fields: t => ({
permissions: t.exposeStringList('permissions'),
members: t.relatedConnection('members', { cursor: 'id', totalCount: true }),
}),
findUnique: id => ({ id }),
id: { resolve: userGroup => userGroup.id },
include: { members: true },
}); Queries builder.queryType({
fields: t => ({
user: t.prismaField({
type: 'User',
nullable: true,
args: {
id: t.arg.id({ required: true }),
},
resolve: (query, root, args) => {
return prismaClient.user.findUnique({
...query,
rejectOnNotFound: true,
where: { id: decodeGlobalID(args.id as string).id },
});
},
}),
users: t.prismaConnection({
type: 'User',
cursor: 'id',
resolve: query => {
return prismaClient.user.findMany({ ...query });
},
}),
}),
}); These queries will work fine: {
users {
edges {
node {
permissions
}
}
}
} {
user(id: "VXNlcjpja3Z4eW01a3kwMDAwcmdnNWV1Z2ltYTVo") {
permissions
}
} This query wont: {
node(id: "VXNlcjpja3Z4eW01a3kwMDAwcmdnNWV1Z2ltYTVo") {
... on User {
permissions
}
}
} Only when I also query for the relation, like so: {
node(id: "VXNlcjpja3Z4eW01a3kwMDAwcmdnNWV1Z2ltYTVo") {
... on User {
permissions
groups {
totalCount
}
}
}
} will it work. Is there a way I can build this, so the consumer doesn't have to include the relation in the query themselves? |
Beta Was this translation helpful? Give feedback.
Answered by
hayes
Feb 16, 2022
Replies: 1 comment 3 replies
-
This looks like a bug |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
hayes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks like a bug