@afosto/graphql-client
TypeScript icon, indicating that this package has built-in type declarations

3.0.0-alpha.5 • Public • Published

Afosto

Afosto GraphQL Client

npm version License

This library provides a GraphQL client which by default communicates with the Afosto GraphQL endpoint.

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
[]

The other configuration options are the options that are available on the graphql-request GraphQLClient.

import { GraphQLClient } from '@afosto/graphql-client';

const client = new GraphQLClient({
  // 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 { GraphQLClient } from '@afosto/graphql-client';

const client = new GraphQLClient({
  // 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

Package Sidebar

Install

npm i @afosto/graphql-client

Weekly Downloads

47

Version

3.0.0-alpha.5

License

MIT

Unpacked Size

108 kB

Total Files

6

Last publish

Collaborators

  • rapid0o
  • gijsbotje
  • tdahorsten