-
Hi! New to Pothos. I'm using the prisma plugin, for context. I have a query which ideally will look like this: query {
viewer {
user {
emailAddress
}
featured(input: { featuredOn: HOME }) {
subjects {
body
}
questions {
body
}
}
}
} What's the best way to model and resolve these fields in Pothos? I think from reading the docs that the best route is to break the Thanks, and thanks for Pothos! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This question is pretty vague, there are a lot of ways to implement things in Pothos, and the best options depend a lot on your data sources. There is an example of a viewer type in one of the tests you could look at as a starting place: There is a lot going on in that schema because it's used for testing, but shows a few different ways you can create user, viewers and other types. Hope this helps, but if you have more specific questions, I'm happy to try to answer |
Beta Was this translation helpful? Give feedback.
-
Thanks @hayes! I did look through that schema example specifically (and just did again) – I think the biggest difference I'm dealing with is that my desire is to nest const FeaturedInput = builder.inputType('FeaturedInput', {
fields: (t) => ({
featuredOn: t.field({ type: FeaturedLabel, required: false }),
}),
});
builder.objectType('FeaturedContent', {
description: 'Currently featured assets of various types',
fields: (t) => ({
subjects: t.field({
type: ['Subject'],
nullable: false,
resolve: async (parent) =>
await prisma.instantCoachingQuestionSubject.findMany({
where: {
featuredOn: {
some: {
label: parent.featuredOn,
},
},
},
}),
}),
platforms: t.prismaField({
type: ['TargetPlatform'],
nullable: false,
resolve: async (query, parent) =>
await prisma.targetPlatform.findMany({
...query,
where: {
featuredOn: {
some: {
label: parent.featuredOn,
},
},
},
}),
}),
}),
});
builder.objectType('Viewer', {
description: 'The current user, session, and other relevant information.',
fields: (t) => ({
user: t.field({
type: 'User',
nullable: true,
resolve: async (_parent, _args, ctx) => ctx.session.user
? await prisma.user.findUnique({
rejectOnNotFound: true,
where: { id: ctx.session.user.id },
})
: null,
}),
featured: t.field({
type: 'FeaturedContent',
nullable: false,
args: {
input: t.arg({ type: FeaturedInput, required: false }),
},
resolve: async (_parent, args) => ({
featuredOn: args.input?.featuredOn ?? FeaturedLabel.HOME,
subjects: [],
platforms: [],
}),
}),
}),
});
builder.queryType({
fields: (t) => ({
viewer: t.field({
type: 'Viewer',
nullable: false,
resolve: async () => ({}),
}),
})
}) |
Beta Was this translation helpful? Give feedback.
Thanks @hayes! I did look through that schema example specifically (and just did again) – I think the biggest difference I'm dealing with is that my desire is to nest
subjects
inside offeatured
inside ofviewer
, and they're not necessarily relations onuser
., and to allow them to take an input which filters the result. Here's the complete solution I ended up with, any obvious issues or improvements I could make? Thanks again!