2

I would like to create a checkout object via the GraphQL API provided by the Saleor eCommerce platform.

According to the gql playground there is a mutation to do so that takes a CheckoutCreateInput object as it's argument.

Here is an example mutation that works fine within the playground.

example gql

Here is the current code that I have tried (I am doing this within a vuex action)

export const actions = {
  addToCart({ commit, dispatch }, cartItem) {
    const currentCartItems = this.state.cartItems
    // Check to see if we already have a checkout object
    if (this.state.checkoutId !== '') {
      // Create a new checkout ID
      console.log('creating new checkout object')
      try {
        this.app.apolloProvider.defaultClient
          .mutate({
            mutation: CREATE_CART_MUTATION,
            variables: {
              checkoutInput: {
                lines: { quantity: 10, variantId: 'UHJvZHVjdFZhcmlhbnQ6NQ==' },
                email: '[email protected]'
              }
            }
          })
          .then(({ data }) => {
            console.log(data)
          })
      } catch (e) {
        console.log(e)
      }
    } else {
      console.log('checkout id already set')
    }

    // TODO: Check to see if the cart already contains the current Cart Item

    commit('ADD_CART_ITEM', cartItem)
  }

and here is the CREATE_CART_MUTATION:

import gql from 'graphql-tag'

export const CREATE_CART_MUTATION = gql`
  mutation($checkoutInput: CheckoutCreateInput!) {
    checkoutCreate(input: $checkoutInput) {
      checkout {
        id
        created
        lastChange
        lines {
          id
          variant {
            id
            name
          }
          quantity
          totalPrice {
            gross {
              localized
            }
            net {
              localized
            }
          }
        }
        totalPrice {
          gross {
            localized
          }
          net {
            localized
          }
        }
      }
    }
  }
`

On the server this comes back with the following error:

graphql.error.base.GraphQLError: Variable "$checkoutInput" got invalid value {"email": "[email protected]", "lines": {"quantity": 10, "variantId": "UHJvZHVjdFZhcmlhbnQ6NQ=="}}.
In field "lines": In element #0: Expected "CheckoutLineInput", found not an object.

1 Answer 1

3

Looks like I was most of the way there, I was just passing a single lines object rather than an array of them. The correct code is as follows:

try {
  this.app.apolloProvider.defaultClient
    .mutate({
      mutation: CREATE_CART_MUTATION,
      variables: {
        checkoutInput: {
          lines: [
            { quantity: cartItem.quantity, variantId: cartItem.variantId }
          ],
          email: '[email protected]'
        }
      }
    })
    .then(({ data }) => {
      console.log('mutation done!')
      commit('SET_CHECKOUT_OBJECT', data.checkoutCreate.checkout)
    })
} catch (e) {
  console.log('error:')
  console.log(e)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.