Prisma + Relay with Intefaces/Unions #656
Replies: 2 comments 4 replies
-
I was able to achieve what I wanted by adding the Node ref to the interface but Typescript still complains about the typing. const Node = builder.nodeInterfaceRef();
export const Activity = builder.interfaceRef<Activity | Event | Entry>("Activity");
Activity.implement({
interfaces: [Node],
resolveType: (root) => {
// receive activity data (prisma.activity)
// parse activity relation & resolve Event or Entry otherwise Error...
},
fields: (t) => ({
// Common fields
}),
});
// use Activity like so
...
activities: t.connection({
type: Activity,
canRefetch: true,
resolve: async (_root, args, { prisma }) =>
// @ts-expect-error: typing
resolveCursorConnection(
{ args, toCursor: ({ id }) => id },
// Manually defining the arg type here is required
// so that typescript can correctly infer the return value
async ({
before,
after,
limit,
inverted,
}: ResolveCursorConnectionArgs) =>
prisma.activity.findMany({
include: {
event: true,
entry: true,
},
take: limit,
where: {
createdAt: {
lt: before,
gt: after,
},
},
orderBy: {
createdAt: inverted ? "desc" : "asc",
},
})
),
}),
... The problem now is that the |
Beta Was this translation helpful? Give feedback.
-
import { builder } from "../builder.js";
import { Activity } from "./interface.js";
import { ActivityCreateInput } from "./input.js";
import { queryFromInfo } from "@pothos/plugin-prisma";
export const Mutation = builder.mutationFields((t) => ({
activity: t.field({
type: Activity,
description: "Create an activity entry",
args: {
data: t.arg({ type: ActivityCreateInput, required: true }),
},
resolve: async (_root, { data }, { prisma, ...ctx }, info) => {
return prisma.activity.create({
...queryFromInfo({
context: { prisma, ...ctx },
info,
// typeName: "Entry", returns an object but for the Prisma `entry` model so it errors here
// path: ["some-field"] returns an empty object regardless of what value is here because returnType Activity returns an empty object
}),
data,
});
},
}),
})); I've encountered a new issue with the interface above & |
Beta Was this translation helpful? Give feedback.
-
I have a hard time making Prisma and Relay work with an interface & was wondering if it's possible or if I'm overcomplicating things & there is a better approach to solving this.
The problem is like so:
I have an
Activity
model which has some data, like which user the activity is for & has relations to other more specific activities such asEntry
&Event
. So I would like to have theseActivity
model acts as an interface for theEvent
&Entry
models to make it possible to query activities like soSo far I have a 'regular' interface using the
builder.interfaceRef().implement()
So far so good, I can use this interface with the
t.field()
with no problem. But I would like to be able to use relay connections. The problem is sinceActivity
is an interface & not aPrismaNode
nor aNode
, it looks like I'm not able to use thet.prismaConnection()
ort.connection({ type: Activity })
helper.I tried using
t.connection()
helper withresolveCursorConnection
(https://pothos-graphql.dev/docs/plugins/relay#creating-connections) as well as making abuilder.connectionObject({ type: Activity })
(https://pothos-graphql.dev/docs/plugins/relay#reusing-connection-objects) but I'm unable to make any of them work correctly.Am I missing something or as mentioned earlier there are simpler ways to achieve the desired query above?
Beta Was this translation helpful? Give feedback.
All reactions