JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 92
  • Score
    100M100P100Q113516F
  • License MIT

Afosto GraphQL client

Package Exports

  • @afosto/graphql-client
  • @afosto/graphql-client/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@afosto/graphql-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 { 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