Mutation request working in postman but not in code

My mutation is formatted exactly the same in my custom code as it is in postman but it keeps returning a 400 error when I try to run it on my server. Not sure if anyone has had this experience but if there is any feed back that would be great! This is what the code looks like

const mutation = mutation {
product_create(
data: {
name: “john”
sku: “john123”
price: “23.00”
warehouse_products: {
warehouse_id: “actualidnotshown”
on_hand: 130
inventory_bin: “Bin A1”
reserve_inventory: 0
replenishment_level: 1
reorder_level: 1
reorder_amount: 20
custom: false
}
value: “15.00”
barcode: “2233443355chair”
country_of_manufacture: “US”
dimensions: { weight: “12”, height: “25”, width: “10”, length: “25” }
kit: false
kit_build: false
no_air: false
final_sale: false
customs_value: “1.00”
not_owned: true
dropship: false
}
) {
request_id
complexity
product {
id
legacy_id
account_id
name
sku
price
value
barcode
country_of_manufacture
dimensions {
weight
height
width
length
}
tariff_code
kit
kit_build
no_air
final_sale
customs_value
customs_description
not_owned
dropship
created_at
}
}
}`

    const options = {

        headers: {
            "Authorization": `Bearer ${accessToken}`,
            "Content-Type": "application/json",
        },
        body: JSON.stringify({ mutation }),

    }

    got
        .post(shipheroUrl, options)
        .then((response) => {
            console.log(response)
        }).catch((err) => {
            console.log(err)
        })
})`

it is also letting me do a query just not a mutation.

Hi @FMCjohn !
Are you still having issues with this?
This is Javascript, right? if that is the case we have a section that could help with GraphQL Client GraphQL Primer – Developer Resources | ShipHero

Thanks in advance!
Tom

Hey FMCJohn,

I had this problem yesterday and it turns out in graphQL it seems EVERYTHING is technically a query. You just gotta wrap your mutation in a query object:
(P.S. avoid using JSON.stringify() as it will add quotes ("") around variable names)

const mutation = `mutation{ my-mutation( ) }`
const query = {
  query: mutation
}
const options = {

        headers: {
            "Authorization": `Bearer ${accessToken}`,
            "Content-Type": "application/json",
        },
        body: query),

    }

If that does work I found writing my mutation on a single line with CSV works well

const mutation = mutation {
product_create(
data: { name: “John”, sku: “john123”, price: “23.00”, warehouse_products: { warehouse_id: “actualidnotshown”, on_hand: 130, inventory_bin: “Bin A1”, reserve_inventory: 0, replenishment_level: 1, reorder_level: 1, reorder_amount: 20, custom: false }, value: “15.00”, barcode: “2233443355chair”, country_of_manufacture: “US”, dimensions: { weight: “12”, height: “25”, width: “10”, length: “25” }, kit: false, kit_build: false, no_air: false, final_sale: false, customs_value: “1.00”, not_owned: true, dropship: false } ) {
request_id
complexity
product {
id
legacy_id
account_id
name
sku
price
value
barcode
country_of_manufacture
dimensions {
weight
height
width
length
}
tariff_code
kit
kit_build
no_air
final_sale
customs_value
customs_description
not_owned
dropship
created_at
}
}
}`
1 Like

Just now seeing this. THANKYOU!!!

2 Likes