Skip to main content

Customer account client

The createCustomerAccountClient function creates a server-only GraphQL client for the Customer Account API. Use the client with gql to query a signed-in customer's profile, orders, and other account data with typed results.

Import createCustomerAccountClient and gql from @shopify/hydrogen/customer-account.

Get the customer's access token from createCustomerSession, and pass the token with each graphql() call.


Creates a client that sends Customer Account API requests from your server. Create the client with the current request's context.

The function throws when the shop ID, API version, or timeout is invalid, when the request context has no HTTPS URL, when no fetch implementation is available, and when the function runs in a browser.

Anchor to input1
input1
required

The shop ID, the API version, the request context, a custom fetch, and the request timeout.

A client with the GraphQL endpoint URL and a graphql() method for Customer Account API requests.

Examples

TypeScript

import type { ShopifyRequestContext } from "@shopify/hydrogen";
import { createCustomerAccountClient, gql } from "@shopify/hydrogen/customer-account";

const CUSTOMER_QUERY = gql(`
query CurrentCustomer {
customer {
firstName
lastName
emailAddress {
emailAddress
}
}
}
`);

export async function getCustomer(
shopId: string,
requestContext: ShopifyRequestContext,
accessToken: string,
) {
const customerAccount = createCustomerAccountClient({ shopId, requestContext });
const result = await customerAccount.graphql(CUSTOMER_QUERY, { accessToken });

if (result.errors) return null;
return result.data.customer;
}

Anchor to Typed queries and mutationsTyped queries and mutations

Creates a typed Customer Account API query or mutation from a GraphQL source string. Pass the document to the Customer Account API client. Call gql as a regular function.

Pass an array of fragment documents as the second argument to append the fragments to the operation. Each fragment must come from this gql function, or the call throws a TypeError.

To validate documents, run hydrogen gql check in your typecheck script with the @shopify/hydrogen/ts-plugin setup. Framework typecheck commands don't validate the documents.

Anchor to source
source
string
required

The GraphQL source of an operation or a fragment.

Anchor to fragments
fragments
readonly []

The fragment documents that the operation uses.

A typed document to pass to the Customer Account API client.

Examples

TypeScript

import { gql } from "@shopify/hydrogen/customer-account";

export const CUSTOMER_QUERY = gql(`
query CustomerDetails {
customer {
firstName
lastName
emailAddress { emailAddress }
}
}
`);

Was this page helpful?