Import
Usage
Therequest
method provides type-safe GraphQL operations with full TypeScript support.
You can leverage generics to type your responses and use GraphQL document nodes for better
integration with third-party libraries.
Basic Usage
Using Variables
You can pass variables to your GraphQL queries and mutations using thevariables
option:
Using String Queries with Type Generics
You can type your GraphQL queries and responses using TypeScript generics:Using GraphQL Document Nodes
For better integration with third-party libraries like Apollo Client or The Guild’s GraphQL Code Generator, you can use GraphQL document nodes created withgql
template literal tags:
- Better IDE support with syntax highlighting and validation
- Integration with code generation tools
- Compatibility with Apollo Client and other GraphQL libraries
Error handling
The SDK will throw errors in GraphQL operations that respond with an errors attribute with length > 0. The error will be an instance ofFetchError<GraphQLResponse>
and will
contain the response body with the errors.
Error
type so if you want to just log the error you can
do so like this:
Interfaces
Client
GraphQL client interface providing methods for executing queries and mutationsProperties
url
Methods
request()
Call Signature
Type Parameters
Type Parameter | Default type |
---|---|
TResponseData | unknown |
TVariables | GraphQLVariables |
Parameters
Parameter | Type | Description |
---|---|---|
request | GraphQLRequest <TVariables > | GraphQL request object containing query and optional variables |
options? | RequestInit | Additional fetch options to apply to the request |
Returns
Promise
<FetchResponse
<GraphQLResponse
<TResponseData
>>>
Promise with the GraphQL response and metadata
Call Signature
Type Parameters
Type Parameter | Default type |
---|---|
TResponseData | - |
TVariables | GraphQLVariables |
Parameters
Parameter | Type | Description |
---|---|---|
document | TypedDocumentNode <TResponseData , TVariables > | TypedDocumentNode containing the query and type information |
variables? | TVariables | Variables for the GraphQL operation |
options? | RequestInit | Additional fetch options to apply to the request |
Returns
Promise
<FetchResponse
<GraphQLResponse
<TResponseData
>>>
Promise with the GraphQL response and metadata
GraphQLError
Represents a GraphQL error returned from the server.Properties
extensions?
Name | Type |
---|---|
code | string |
path | string |
locations?
Name | Type |
---|---|
column | number |
line | number |
message
path?
GraphQLRequest<TVariables>
GraphQL request object used for queries and mutations.Type Parameters
Type Parameter | Default type |
---|---|
TVariables | GraphQLVariables |
Properties
operationName?
query
variables?
GraphQLResponse<TResponseData>
Standard GraphQL response format as defined by the GraphQL specification.Type Parameters
Type Parameter | Default type |
---|---|
TResponseData | unknown |
Properties
data?
errors?
Type Aliases
GraphQLVariables
Functions
createAPIClient()
Parameters
Parameter | Type | Default value | Description |
---|---|---|---|
url | string | undefined | Base URL for the GraphQL endpoint |
chainFunctions | ChainFunction [] | [] | Array of middleware functions for the fetch chain |
Returns
Client
GraphQL client with query and mutation methods