Client Adapters
Client adapters are classes, that act as an interface between LiveMarket SDK and your GraphQL client of choice. You can use a built-in adapter or write a custom one for your GraphQL client.
Built-in adapters
FetchClientAdapter
The FetchClientAdapter is the most basic client adapter you can have. It uses browser’s Fetch API to make requests to LiveMarket. It should really only be used if you work in a no-bundler environment as it’s very primitive.
import { FetchClientAdapter } from 'livemarket-sdk';
const adapter = new FetchClientAdapter();UrqlClientAdapter
This adapter uses the urql client to make requests to the API. You can use it if you already have urql in your project or if you don’t have any GraphQL client and are willing to install it.
import { createClient } from '@urql/core`;
import { UrqlClientAdapter } from 'livemarket-sdk';
const adapter = new UrqlClientAdapter(createClient);Writing your own adapter
Client adapters should implement the ClientAdapterInterface. The library provides a BaseClientAdapter class to handle common tasks required by the interface. If you extend the BaseClientAdapter, you only need to provide 2 methods: query and mutate. See the definition below to find out what you need to do to get your adapter working.
If a user is defined, your query and mutate methods must include the authorization header with the following value: JWT ###token###. The token can be obtained from user.temporaryToken property. user is the name of the property on BaseClientAdapter.
interface ClientAdapterInterface {
query<T>(
query: string,
variables: Record<string, any>,
options?: ClientAdapterRequestOptions
): Promise<T | undefined>;
mutate<T>(
query: string,
variables: Record<string, any>,
options?: ClientAdapterRequestOptions
): Promise<T | undefined>;
setUser(temporaryUser: TemporaryUser): void; // Handled by the BaseClientAdapter
}
type ClientAdapterRequestOptions = {
noCache?: boolean;
};