I am having success using the built-in schema for basic CRUD tasks on individual classes. Thank you for adding this remarkable graphql feature set to parse-server!
I have been reading here and here about creating custom schema to use with my app. I’ve also had a look at Moumouls’ next-atomic-gql-server repo. Unfortunately, I’m not gleaning enough from these resources to move forward on my own so I’m looking for some help.
I have these browser classes on my parse-server:
-
User - has typical fields plus a
cart
field having a ‘relation’ withItem
class. -
Item - has typical fields (title, description, price, …) plus
user
field having apointer
toUser
class. -
CartItem - has similar field to
Item
class plususer
field having apointer
toUser
class and anitem
field having apointer
toItem
class.
I would like to create an addToCart
mutation:
# from schema.graphql
type Mutation {
addToCart(id: ID!): CartItem
}
# in the client
mutation ADD_TO_CART_MUTATION ($id: ID!) {
addToCart(id: $id) {
cartItem {
id
quantity
}
}
}
As an example, I have a mutation resolver example from GraphQL Yoga (using express.js) that I’d like to emulate:
async addToCart(parent, args, ctx, info) {
// 1. query the user's current cart
const [existingCartItem] = await ctx.db.query.cartItems({
where: {
user: { id: userId },
item: { id: args.id },
},
})
// 2. check if that item is already in their cart and increment by one if it is
if (existingCartItem) {
return ctx.db.mutation.updateCartItem({
where: { id: existingCartItem.id },
data: { quantity: existingCartItem.quantity + 1 },
}, info);
}
// 3. if it's not, create a fresh cartItem for that user
return ctx.db.mutation.createCartItem({
data: {
item: {
connect: { id: args.id },
},
user: {
connect: { id: userId },
},
},
}, info);
},
How is a custom mutation like addToCart
, that involves first calling another query, then doing a conditional check before branching to one of two other mutations, created in parse-server?