-
Is it possible to access parent resolver args? E.g. how would I access the {
cart(id: "59ca922e-a045-445a-9438-f03df4901b0a", currency: "EUR") {
id
subTotal {
amount
formatted
}
items {
id
name
quantity
lineTotal {
amount
formatted
}
unitTotal {
amount
formatted
}
}
}
} import SchemaBuilder from '@pothos/core'
import WithInputPlugin from '@pothos/plugin-with-input'
import z from 'zod'
const CartItem = z.object({
id: z.string().uuid(),
name: z.string(),
price: z.number(),
quantity: z.number(),
})
const Cart = z.object({
id: z.string().uuid(),
items: z.array(CartItem).optional(),
})
const builder = new SchemaBuilder<{
Objects: {
Cart: z.infer<typeof Cart>
CartItem: z.infer<typeof CartItem>
}
Scalars: {
ID: { Input: string; Output: string }
}
}>({
plugins: [WithInputPlugin],
})
const CARTS = [
{
id: '59ca922e-a045-445a-9438-f03df4901b0a',
items: [
{ id: 'c4543662-38f5-450d-b8e3-fcd45d1f6149', name: 'My item', price: 1000, quantity: 1 },
],
},
]
const Money = builder.objectRef<number>('Money').implement({
fields: t => ({
amount: t.int({ resolve: amount => amount }),
formatted: t.string({
resolve: amount =>
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
}).format(amount / 100),
}),
}),
})
builder.objectType('Cart', {
fields: t => ({
id: t.exposeString('id'),
items: t.field({
type: ['CartItem'],
resolve: cart => cart.items ?? [],
}),
subTotal: t.field({
type: Money,
resolve: cart =>
cart.items?.reduce(
(acc, item) => acc + item.price * item.quantity,
0,
) ?? 0,
}),
}),
})
builder.objectType('CartItem', {
fields: t => ({
id: t.exposeString('id'),
name: t.exposeString('name'),
quantity: t.exposeInt('quantity'),
lineTotal: t.field({
type: Money,
resolve: item => item.price * item.quantity,
}),
unitTotal: t.field({
type: Money,
resolve: item => item.price,
}),
}),
})
builder.queryType({
fields: t => ({
cart: t.field({
nullable: true,
args: {
id: t.arg.id({ required: true }),
currency: t.arg.string({ required: false, defaultValue: 'USD' }),
},
type: 'Cart',
resolve: (_, { id }) => {
const cart = CARTS.find(c => c.id === id)
if (!cart)
throw new Error(`No cart with ID: ${id}`)
return cart
},
}),
}),
})
builder.mutationType({
fields: t => ({
addItem: t.fieldWithInput({
input: {
cartId: t.input.id({ required: true }),
id: t.input.id({ required: true }),
name: t.input.string({ required: true }),
price: t.input.int({ required: true }),
quantity: t.input.int({ defaultValue: 1, required: true }),
},
type: 'Cart',
resolve: (_, { input: { cartId, ...input } }) => {
const cart = CARTS.find(c => c.id === cartId)
if (!cart)
throw new Error(`No cart with ID: ${cartId}`)
return {
id: cartId,
items: [...cart?.items, input],
}
},
}),
}),
})
export default builder.toSchema() |
Beta Was this translation helpful? Give feedback.
Answered by
hayes
Sep 7, 2023
Replies: 1 comment 2 replies
-
There isn't a great way to do this. This is more of a general GraphQL issue than something specific to pothos. There are 2 ways you can get those arguments:
I don't currently have a better suggestion |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
codepunkt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There isn't a great way to do this. This is more of a general GraphQL issue than something specific to pothos.
There are 2 ways you can get those arguments:
info
object (4th argument of a resolver). This isn't straight forward, and requires you to manually find the right selection, and then use thegetArgumentValues
helper fromgraphql
to extract the arguments. This also will bypass certain things like validation, and parsing logic in resolvers, for example the decoding of glo…