1.0.0 • Published 5 months ago
@untools/gql-client v1.0.0
GraphQL Client
A simple and lightweight GraphQL client for making requests in JavaScript/TypeScript applications.
Installation
npm install @untools/gql-client
# or
yarn add @untools/gql-client
Usage
Basic usage
import { executeGraphQL } from '@untools/gql-client';
const GET_USER_QUERY = `#graphql
query GetUser($id: ID!) {
getUser(id: $id) {
id
name
email
}
}
`;
const getUser = async (userId: string) => {
return executeGraphQL({
query: GET_USER_QUERY,
variables: { id: userId },
url: 'https://your-api-url.com/graphql',
headers: {
'auth-token': 'your-auth-token'
}
});
};
Using the GraphQLClient class
import { GraphQLClient } from '@untools/gql-client';
const client = new GraphQLClient({
apiUrl: 'https://your-api-url.com/graphql',
apiKey: 'your-api-key'
});
const GET_USER_QUERY = `#graphql
query GetUser($id: ID!) {
getUser(id: $id) {
id
name
email
}
}
`;
const getUser = async (userId: string, token: string) => {
return client.executeGraphQL({
query: GET_USER_QUERY,
variables: { id: userId },
headers: {
'auth-token': token
}
});
};
Custom logger
import { GraphQLClient } from '@untools/gql-client';
const client = new GraphQLClient({
apiUrl: 'https://your-api-url.com/graphql',
apiKey: 'your-api-key',
logger: {
log: (...args) => console.log('[GraphQL Client]', ...args),
error: (...args) => console.error('[GraphQL Client]', ...args)
}
});
API Reference
GraphQLClient
new GraphQLClient(config: GraphQLClientConfig)
GraphQLClientConfig
apiUrl
: Default GraphQL API URLapiKey
: Default API keylogger
: Custom logger object withlog
anderror
methods
executeGraphQL
executeGraphQL<TResponse, TVariables>({
query: string,
variables: TVariables,
headers?: Record<string, string>,
url?: string
}): Promise<TResponse>
graphqlRequest
graphqlRequest<T>(
url: string,
options: GraphQLRequestOptions,
headers?: Record<string, string>,
apiKey?: string
): Promise<GraphQLResponse<T>>
License
MIT
1.0.0
5 months ago