Skip to content

Instantly share code, notes, and snippets.

@alexphelps
Created November 30, 2022 04:10
Show Gist options
  • Save alexphelps/037f33b9b5f35017dddcbc00f5bf95ce to your computer and use it in GitHub Desktop.
Save alexphelps/037f33b9b5f35017dddcbc00f5bf95ce to your computer and use it in GitHub Desktop.
29 Next Storefront API Guide

Storefront GraphQL API

GraphQL Endpoint - https://<store domain>/api/graphql/

Use the GraphiQL browser client to write, validate, and test your GraphQL queries.

Customer

me

Returns current user account information.

{
  me {
    id
    firstName
    lastName
    metadata
    pk
    email
  }
}

register

mutation {
  register(
    input: {email: "<EMAIL>", password: "<PASSWORD>", language: en, acceptsMarketing: true}
  ) {
    success
    errors
  }
}

tokenAuth

Returns a JWT authentication token to make authenticated requests.

mutation {
  tokenAuth(input: {email: "<USER EMAIL>", password: "<USER PASSWORD>"}) {
    success
    user {
      id
      email
      firstName
      lastName
    }
    errors
    token
  }
}

verifyToken

Verify a JWT token is valid, used to check if need to ask the current user needs to re-authenticate.

mutation {
  verifyToken(
    input: {token: "<JWT TOKEN>"}
  ) {
    success
    payload
  }
}

updateAccount

Update the current authenticated user account. Requires Authorization header with valid JWT token

mutation {
  updateAccount(input: {lastName: "Example", acceptsMarketing: false}) {
    success
    errors
  }
}

Use the tokenAuth endpoint to get a valid JWT token.

{
  "Authorization": "JWT <AUTH TOKEN>"
}

Cart

createCart

Create a new cart, also allows you to get the ID of the current cart by passing an empty object to input, ie input:{}.

mutation {
  createCart(
    input: {lines: [{productPk: 1, quantity: 1}], replace: true, attribution: {utmSource: "test12123"}}
  ) {
    cart {
      id
      pk
      totalInclTax
      totalExclTax
      lines {
        pk
        unitPriceExclTax
        linePriceExclTax
        linePriceExclTaxInclDiscounts
        discountValue
        attributes {
          option
          value
        }
      }
      attribution {
        utmSource
      }
      user {
        email
      }
    }
    success
    errors
  }
}

addCartLines

Add line items to a cart.

mutation {
  addCartLines(
    input: {cartId: "Q2FydE5vZGU6ODExMDU=", lines: [{productPk: 1, quantity: 2, subscription: {interval: "week", intervalCount: 8}}]}
  ) {
    cart {
      id
      pk
      totalInclTax
      totalExclTax
      numLines
      numItems
      status
      offerDiscounts {
        name
        description
        amount
      }
      lines {
        pk
        linePriceInclTax
        discountValue
        attributes {
          option
          value
        }
      }
    }
    success
    errors
  }
}

updateCartAttribution

Udpate the attribution for a cart.

mutation{
  updateCartAttribution(input:{
    cartId: "Q2FydE5vZGU6ODExMDU=",
    attribution: {
      utmSource: "TEST",
      utmMedium: "aa",
    }
  }){
    cart{
      attribution{
        utmSource,utmTerm,utmMedium, metadata
      }
    }
  }
}

emptyCart

Empty a cart of all its line items.

mutation{
  emptyCart(input:{cartId:"Q2FydE5vZGU6ODExMDU="}){
    success,
    errors,
    cart{
      numItems, numItems
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment