customer
Get current customer information
Signature
declare function customer(
context: Context,
customQuery?: CustomQuery,
customHeaders?: CustomHeaders
): Promise<ApolloQueryResult<CustomerQuery>>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
context | Required | Context | |
customQuery | Optional | CustomQuery | |
customHeaders | Optional | CustomHeaders |
Referenced Types
- Context
- CustomQuery
- CustomHeaders
ApolloQueryResultCustomerQuery
Examples
The customer() returns the currently active user. This means that the request needs to contain an authorization token, which will tell Magento whose customer data should be fetched.
If your browser has a VSF customer cookie saved, you can just call customer() without any parameters - the token will be attached automatically on every request to the middleware.
Simple usage:
import { sdk } from '~/sdk.config.ts';
// fetch customer
const customer = await sdk.magento.customer();If you're calling customer() in a non-browser context (for example in integration tests) where it's not possible to save a cookie, you can attach the token manually using customHeaders
Usage with manual authorization:
import { sdk } from '~/sdk.config.ts';
const token = '123'
const customHeaders = { Authorization: `Bearer {token}` }
// fetch customer
const customer = await sdk.magento.customer({ customHeaders });Creating a custom GraphQL query for fetching customer
module.exports = {
integrations: {
magento: {
customQueries: {
'customer-custom-query': ({ variables, metadata }) => ({
variables,
query: `
query customer {
customer {
${metadata.fields}
}
}
`
}),
},
}
}
};Using a custom GraphQL query to fetch customer
import { sdk } from '~/sdk.config.ts';
// reduce the amount of fields returned by the query, when compared to the default query
const customQuery = {
customer: 'customer-custom-query',
metadata: {
fields: 'email firstname lastname'
}
};
const customer = await sdk.magento.customer(customQuery);
// customer will contain only the fields specified in the custom query.