Crystallize logo
Documentation/Developer Guides/Order API/Get All Orders by a Customer

Get All Orders by a Customer

With the Crystallize Order API, you can retrieve all the orders made by a particular customer. This section will show you how.

In order to perform these operations, ensure that you have the proper authentication (if necessary) and user permissions.

Fetch All Orders

In order to fetch all orders by a customer, you’ll need to provide the customer identifier to the query. The query below uses getAll and provides the customerIdentifier argument to it. It then fetches information regarding all the orders the customer has placed until now.

query {
  orders {
    getAll(customerIdentifier: "cus_0001") {
      edges {
        node {
          cart {
            name
            quantity
            price {
              gross
            }
          }
        }
      }
    }
  }
}
Running this query requires access tokens. If you have already generated the tokens, you can test out the query using the order API endpoint.

Slicing Results

You can slice the list of orders you get back by using the first argument. This argument allows you to fetch only the number of orders you specify. Let's say you only want to grab the first three results. This is how you would do it:

query {
  orders {
    getAll(customerIdentifier: "cus_0001", first: 3) {
      edges {
        node {
          cart {
            name
          }
        }
      }
    }
  }
}
Running this query requires access tokens. If you have already generated the tokens, you can test out the query using the order API endpoint.

GraphQL Connections

A connection in GraphQL acts as a wrapper for the list of results you’re paginating. Any connection consists of two fields: edges and pageInfo. Take a look at the following query:

query {
  orders {
    getAll(customerIdentifier: "cus_0001") {
      edges {
        node {
          cart {
            name
          }
        }
        cursor
      }
      pageInfo {
        totalNodes
        hasNextPage
      }
    }
  }
}
Running this query requires access tokens. If you have already generated the tokens, you can test out the query using the order API endpoint.

The query above contains the following:

  • edges: contains a list of OrderConnectionEdge types.
  • node: the object containing the information about an order.
  • cursor: a string specifying the location of an item in the list.
  • pageInfo: this contains fields such as hasNextPage, hasPreviousPage, endCursor, startCursor, and totalNodes.

Using Pagination

To apply pagination to the list of orders, use the first argument and the after argument, which is the cursor. The following query fetches the first two items, starting from the cursor value provided to it.

query {
  orders {
    getAll(
      customerIdentifier: "cus_0001"
      first: 2
      after: "ADD_CURSOR_VALUE_HERE"
    ) {
      edges {
        node {
          cart {
            name
          }
        }
        cursor
      }
      pageInfo {
        totalNodes
        hasNextPage
      }
    }
  }
}
Running this query requires access tokens. If you have already generated the tokens, you can test out the query using the order API endpoint.