-
All I want to do is add an item to the items array in my Cart object.
In this code I am using the pothos withinput plugin: link to docs I have tried:
const THE_TYPE = gql`input addItemInput {
cartId: String!
id: String!
name: String!
price: Float!
}
`
const MUTATION = gql`
mutation AddItem($input: MutationAddItemInput) {
addItem(input: $input){carts{
id
items{
name
}}}}
`; *When I run the following mutation in my graphiql interface it works:mutation MyMutation{
addItem(input:{
cartId: "2",
id: "12",
name: "New Item!",
price: 1900,
}){
items{
name
}
}} However when I run the mutation below I get a 400 error:Error: Response not successful: Received status code 400 import { useQuery, gql, useMutation } from '@apollo/client';
export default function DisplayCarts() {
interface Cart {
id: string;
items: string[];
}
interface Items {
}
const GET_CARTS = gql`
query {
carts{
id
items{
name
}}} `;
const MUTATION = gql`
mutation AddItem($input: Any) {
addItem(input: $input){
carts{
id
items{
name
}}
}}`;
const { loading, error, data } = useQuery(GET_CARTS)
const [addItem] = useMutation(MUTATION, {
refetchQueries: [{ query: GET_CARTS }]
// update(cache, { data: { addItem } }) {
// addItem is the response of the query of add item function
// console.log(data);
// @ts-ignore
// const { carts } = cache.readQuery({ query: GET_CARTS });
// cache.writeQuery({
// query: GET_CARTS,
// data: { carts: [...carts, addItem] }
// })
// }
})
function AddTodo() {
let theInput = {
cartId: "2",
id: "12",
name: "New Item!",
price: 1900,
quantity: 2
}
// @ts-ignore
addItem({ variables: { input: theInput } });
}; Here is my backend resolver function using pothosKeep in mind my query does work in my graphiql interface so the issue is probably not on the backend builder.mutationType({
fields: (t) => ({
addItem: t.fieldWithInput({
input: {
cartId: t.input.string({ required: true }),
id: t.input.string({ required: true }),
name: t.input.string({ required: true }),
price: t.input.int({ required: true }),
quantity: t.input.int({ required: true, defaultValue: 1 }),
},
type: Cart,
resolve: (_, { input: { cartId, ...input } }) => {
const cart = CARTS.find((cart) => cart.id === cartId);
if (!cart) {
throw new Error(`Cart with id ${cartId} not found`)
}
return {
id: cartId,
items: [...cart?.items, input]
}
}
}),
}),
}) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I'm not sure what this is doing, but the Any type there doesn't really make sense. You probably just need to replace that with the name of your input type: |
Beta Was this translation helpful? Give feedback.
-
Thank you the problem was the way that pothos automatically formats the types when using the with-input pluginFrom the docs
This will produce a schema like:
Here is the correct mutation for my case:
|
Beta Was this translation helpful? Give feedback.
Thank you the problem was the way that pothos automatically formats the types when using the with-input plugin
From the docs
This will produce a schema like:
Here is the correct mutation for my case: