Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

shopify storefront checkout api

shopify storefront checkout api

MartinVod
Visitor
3 0 0

Hey, I'm building a front using react and i got to the checkout.

when im fetching to create checkout as the example query everything works just fine: (using a string and not variables).

mutation {
  checkoutCreate(input: {
    lineItems: [{ variantId: "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMzg3MDQ4MzI3NTc5OA==", quantity: 1 }]
  }) {
    checkout {
       id
       webUrl
       lineItems(first: 5) {
         edges {
           node {
             title
             quantity
           }
         }
       }
    }
  }
}

 

when i try fethcing with variables (array of objects contain the lineItem Id and quantity) i get an error:

that's my array:

MartinVod_0-1618984547256.png

that's my query:

                const query = `mutation checkoutCreate($lineItems: (CheckoutLineItemConnection!)){
                    checkoutCreate(input: {
                        lineItems: $lineItems
                        }) {
                            checkout {
                                id
                                webUrl
                                lineItems(first: 2) {
                                edges {
                                    node {
                                        title
                                        quantity
                                    }
                                }
                            }
                        }
                    }
                }`

 

I know i have a syntax problem with the query im just not sure where. (my graphql understainding is limited).

any help will be much appriciated.

 

Replies 2 (2)

MartinVod
Visitor
3 0 0

small update: i found the query i need and updated it to: 

                const query = `mutation checkoutCreate($lineItems: [CheckoutLineItemInput!]!){
                    checkoutCreate(input: {
                        lineItems: $lineItems
                        }) {
                            checkout {
                                id
                                webUrl
                                lineItems(first: 9) {
                                edges {
                                    node {
                                        title
                                        quantity
                                    }
                                }
                            }
                        }
                    }
                }`

 

but i still recieve an error Variable $lineItems of type [CheckoutLineItemInput!]! was provided invalid value

 

c10s
Shopify Partner
67 12 27

Here's a mutation and variable that should work (you needed a lineItems key in your variable):

 

mutation checkoutCreate($lineItems: [CheckoutLineItemInput!]) {
  checkoutCreate(input: {
    lineItems: $lineItems
  }) {
    checkout {
      id
      webUrl
      lineItems(first: 9) {
        edges {
          node {
            title
            quantity
          }
        }
      }
    }
  }
}

 

Your lineItems variable should be shaped like this:

 

{
  "lineItems": [
    {"variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8zMjUxMjY5NDg0NTU0MA==", "quantity": 1}
  ]
}

 

 

I'd personally go with just passing the entire input though, rather than just the line items:

 

mutation checkoutCreate($input: CheckoutCreateInput!) {
  checkoutCreate(input: $input) {
    checkout {
      id
      webUrl
      lineItems(first: 9) {
        edges {
          node {
            title
            quantity
          }
        }
      }
    }
  }
}
{
  "input": {
    "lineItems": [
      {"variantId": "Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8zMjUxMjY5NDg0NTU0MA==", "quantity": 1}
    ]
  }
}