@afosto/graphql-client
v3.0.0
Published
Afosto GraphQL client
Downloads
219
Readme
Installation
# Install with Yarn
yarn add @afosto/graphql-client graphql
# Install with NPM
npm install @afosto/graphql-client graphql
Basic usage
import { graphQLClient, gql } from '@afosto/graphql-client';
// For authentication
graphQLClient.setAuthorizationHeader('AUTH_TOKEN');
// Your graphQL query and variables.
const query = gql``;
const variables = {};
// Request
await graphQLClient.request(query, variables);
Custom configuration
If you would like to use the GraphQLClient with other configuration than the default configuration.
| Option | Description | Default |
|-----------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------|
| url | The base url for the client. | https://afosto.app/graphql |
| convertResponseToCamelCase | Whether to convert the response data to camel case. | true |
| convertVariablesToSnakeCase | Whether to convert the variables to snake case. | true | |
| excludeConversionKeys | Set the keys that should be excluded from being converted to camel case and snake case. | [] | |
| stopPaths | Exclude children at the given object paths in dot-notation from being converted to camel case. Note: Is not yet implemented for snake case conversion | [] | |
import { createGraphQLClient } from '@afosto/graphql-client';
const client = createGraphQLClient({
// Your configuration.
});
// For authentication
client.setAuthorizationHeader('AUTH_TOKEN');
// Your graphQL query and variables.
const query = gql``;
const variables = {};
// Request
await client.request(query, variables);
Default response transformation
By default the Afosto GraphQLClient will transform the response keys to camel case and the variable keys to snake case. If you would like to change these settings, you can do the following:
Default client
graphQLClient.setConvertResponseToCamelCase(false);
graphQLClient.setConvertVariablesToSnakeCase(false);
Custom client
import { createGraphQLClient } from '@afosto/graphql-client';
const client = createGraphQLClient({
// Your configuration.
convertResponseToCamelCase: false,
convertVariablesToSnakeCase: false,
});
// Or after client initialization
client.setConvertResponseToCamelCase(false);
client.setConvertVariablesToSnakeCase(false);
Custom response transformation
If you would like to alter the response data yourself, you can use the following:
await graphQLClient.request(query, variables, {
transformResponse: [
response => {
// Alter and return your new response data.
return response;
},
],
});
Compatibility
- Node >= 18