Class: Client

Client(typeBundle, options)

The Client class used to create and send GraphQL documents, fragments, queries and mutations.

Constructor

new Client(typeBundle, options)

Parameters:
Name Type Description
typeBundle Object

A set of ES6 modules generated by graphql-js-schema.

options Object

An options object. Must include either url and optional fetcherOptions OR a fetcher function.

Properties
Name Type Attributes Default Description
url|fetcher String | function

Either the URL of the GraphQL API endpoint, or a custom fetcher function for further customization.

fetcherOptions Object <optional>

Additional options to use with fetch, like headers. Do not specify this argument if fetcher is specified.

registry ClassRegistry <optional>
new ClassRegistry()

A ClassRegistry used to decode the response data.

Source:

Methods

document() → {Document}

Creates a GraphQL document.

Source:
Returns:

A GraphQL document.

Type
Document
Example
const document = client.document();

enum(key) → {Enum}

Creates an enum to be used in a Query or Mutation.

Parameters:
Name Type Description
key String

The key of the enum.

Source:
Returns:

An enum object that can be used in a Query or Mutation.

Type
Enum
Example
const titleEnum = client.enum('TITLE');

fetchAllPages(paginatedModels, options) → {Promise.<Array.<GraphModel>>}

Fetches all subsequent pages of a paginated array of nodes.

Parameters:
Name Type Description
paginatedModels Array.<GraphModel>

The list of nodes on which to fetch all pages.

options Object

Options object containing:

Properties
Name Type Description
pageSize Integer

The number of nodes to query on each page.

Source:
Returns:

A promise resolving with all pages of GraphModels, including the original list.

Type
Promise.<Array.<GraphModel>>
Example
client.fetchAllPages(nodes, {pageSize: 20}).then((result) => {
  // Do something with all the models
  console.log(result);
});

fetchNextPage(nodeOrNodes, optionsopt) → {Promise.<Array.<GraphModel>>}

Fetches the next page of a paginated node or array of nodes.

Parameters:
Name Type Attributes Description
nodeOrNodes GraphModel | Array.<GraphModel>

The node or list of nodes on which to fetch the next page.

options Object <optional>

Options object containing:

Properties
Name Type Attributes Description
first Integer <optional>

The number of nodes to query on the next page. Defaults to the page size of the previous query.

Source:
Returns:

A promise resolving with the next page of GraphModels.

Type
Promise.<Array.<GraphModel>>
Example
client.fetchNextPage(node, {first: 10}).then((result) => {
  // Do something with the next page
  console.log(result);
});

mutation(nameopt, variablesopt, selectionSetCallback) → {Mutation}

Creates a GraphQL mutation.

Parameters:
Name Type Attributes Description
name String <optional>

A name for the mutation.

variables Array.<VariableDefinition> <optional>

A list of variables in the mutation. See Client#variable.

selectionSetCallback function

The mutation builder callback. A SelectionSet is created using this callback.

Source:
Returns:

A GraphQL mutation.

Type
Mutation
Example
const input = client.variable('input', 'CatCreateInput!');

const mutation = client.mutation('myMutation', [input], (root) => {
  root.add('catCreate', {args: {input}}, (catCreate) => {
    catCreate.add('cat', (cat) => {
      cat.add('name');
    });
  });
});

query(nameopt, variablesopt, selectionSetCallback) → {Query}

Creates a GraphQL query.

Parameters:
Name Type Attributes Description
name String <optional>

A name for the query.

variables Array.<VariableDefinition> <optional>

A list of variables in the query. See Client#variable.

selectionSetCallback function

The query builder callback. A SelectionSet is created using this callback.

Source:
Returns:

A GraphQL query.

Type
Query
Example
const query = client.query('myQuery', (root) => {
  root.add('cat', (cat) => {
   cat.add('name');
  });
});

refetch(nodeType) → {Promise.<GraphModel>}

Refetches a GraphModel whose type implements Node.

Parameters:
Name Type Description
nodeType GraphModel

A GraphModel whose type implements Node.

Source:
Returns:

The refetched GraphModel.

Type
Promise.<GraphModel>
Example
client.refetch(node).then((result) => {
  // Do something with the refetched node
  console.log(result);
});

send(request, variableValuesopt, otherPropertiesopt, headersopt) → {Promise.<Object>}

Sends a GraphQL operation (query or mutation) or a document.

Parameters:
Name Type Attributes Default Description
request Query | Mutation | Document | function

The operation or document to send. If represented as a function, it must return Query, Mutation, or Document and recieve the client as the only param.

variableValues Object <optional>
null

The values for variables in the operation or document.

otherProperties Object <optional>
null

Other properties to send with the query. For example, a custom operation name.

headers Object <optional>
null

Additional headers to be applied on a request by request basis.

Source:
Returns:

A promise resolving to an object containing the response data.

Type
Promise.<Object>
Example
client.send(query, {id: '12345'}).then((result) => {
  // Do something with the returned result
  console.log(result);
});

variable(name, type, defaultValueopt) → {VariableDefinition}

Creates a variable to be used in a Query or Mutation.

Parameters:
Name Type Attributes Description
name String

The name of the variable.

type String

The GraphQL type of the variable.

defaultValue * <optional>

The default value of the variable.

Source:
Returns:

A variable object that can be used in a Query or Mutation.

Type
VariableDefinition
Example
const idVariable = client.variable('id', 'ID!', '12345');