Methods

addCurrentToMenuItem(item)

Add the current class and attributes to a menu item.

Parameters:
NameTypeDescription
itemHTMLElement

The menu item to add the current class and attributes to.

Since
  • 4.0.2

buildArgsString(args, returnParensOnEmpty) → {string}

Build an arguments string from an object for use in Hermes query.

Parameters:
NameTypeDescription
argsobject

The object to convert to a string.

returnParensOnEmptyboolean

Whether to return parentheses around the args string if it is empty.

Since
  • 4.1.0
Returns:

The arguments string.

Type: 
string

buildCacheKey(prefix, paramKeys, params) → {string}

Build a cache key.

Parameters:
NameTypeDescription
prefixstring

The prefix for the cache key.

paramKeysArray

The keys of the parameters to build the cache key.

paramsobject

The parameters to build the cache key.

Since
  • 4.0.2
Returns:

The cache key.

Type: 
string

buildConnectObject(obj) → {object}

Build a connect or disconnect object for use in Hermes mutation.

Parameters:
NameTypeDescription
objobject | Array

The object or array of objects to convert to a connect or disconnect object.

Since
  • 4.1.0
Returns:

The connect or disconnect object.

Type: 
object

buildDeleteObject(obj) → {object}

Build a delete object for use in Hermes mutation.

Parameters:
NameTypeDescription
objobject

The object to convert to a delete object.

Since
  • 4.1.0
Returns:

The delete object.

Type: 
object

buildInsertObject(obj) → {object}

Build an insert object for use in Hermes mutation.

Parameters:
NameTypeDescription
objobject

The object to convert to an insert object.

Since
  • 4.1.0
Returns:

The insert object.

Type: 
object

buildMutationString(mutationObj, mutationType) → {string}

Build a mutation string from a mutation object and mutation type for use in Hermes mutation.

Parameters:
NameTypeDescription
mutationObjobject

The mutation object to convert to a string.

mutationTypestring

The type of mutation to build. Must be one of 'insert', 'update', 'delete', 'connect', or 'disconnect'.

Since
  • 4.1.0
Returns:

The mutation string.

Type: 
string
Example
// Example inserting single company object.
const insertMutationObj = {
  company: {
    objects: [ { name: 'My Business', address: '123 Main St' } ],
    returning: {
      id: true,
      name: true,
      address: true,
    },
  },
};

const insertMutationString = buildMutationString( insertMutationObj, 'insert' );
// insertMutationString = '{insert_company(objects: [{name: "My Business", address: "123 Main St"}]) {returning {id, name, address}}}';


// Example with single company updates.
const updateMutationObj = {
  company: {
    id: 1,
    _args: {
      name: 'My Business',
      address: '123 Main St',
    },
    returning: {
      id: true,
      name: true,
      address: true,
    },
  },
};

const updateMutationString = buildMutationString( updateMutationObj, 'update' );
// updateMutationString = '{update_company(id: 1, name: "My Business", address: "123 Main St") {returning {id, name, address}}}';


// Example deleting single company object.
const deleteMutationObj = {
  company: {
    id: 1,
  },
};

const deleteMutationString = buildMutationString( deleteMutationObj, 'delete' );
// deleteMutationString = '{delete_company(id: 1) {}}';


// Example connecting single company and department objects.
const singleConnectMutationObj = {
  company_department: {
    from: 1,
    to: 2,
  },
};

const singleConnectMutationString = buildMutationString( singleConnectMutationObj, 'connect' );
// connectMutationString = '{connect_company_department(objects: [{from: 1, to: 2}]) {}}';


// Example connecting multiple company and department objects.
const multipleConnectMutationObj = {
  company_department: [
    {
      from: 1,
      to: 2,
    },
    {
      from: 3,
      to: 4,
    },
  ],
};

const multipleConnectMutationString = buildMutationString( multipleConnectMutationObj, 'connect' );
// multipleConnectMutationString = '{connect_company_department(objects: [{from: 1, to: 2}, {from: 3, to: 4}]) {}}';


// Example disconnecting single company and department objects.
const singleDisconnectMutationObj = {
  company_department: {
    from: 1,
    to: 2,
  },
};

const disconnectMutationString = buildMutationString( singleDisconnectMutationObj, 'disconnect' );
// disconnectMutationString = '{disconnect_company_department(objects: [{from: 1, to: 2}]) {}}';


// Example disconnecting multiple company and department objects.
const multipleDisconnectMutationObj = {
  company_department: [
    {
      from: 1,
      to: 2,
    },
    {
      from: 3,
      to: 4,
    },
  ],
};

const multipleDisconnectMutationString = buildMutationString( multipleDisconnectMutationObj, 'disconnect' );
// multipleDisconnectMutationString = '{disconnect_company_department(objects: [{from: 1, to: 2}, {from: 3, to: 4}]) {}}';

buildQueryString(queryObj) → {string}

Build a query string from a query object for use in Hermes query.

Parameters:
NameTypeDescription
queryObjobject

The query object to convert to a string.

Since
  • 4.0.5
Returns:

The query string.

Type: 
string
Example
const queryObj = {
  company: {
    _alias: 'first_ten_companies',
    _args: {
      limit: 10,
    },
    id: true,
    name: {
      _alias: 'business_name',
    },
    address: true,
    department: {
      id: {
        _alias: 'department_id',
      },
      name: true,
    },
  },
};

const queryString = buildQueryString( queryObj );
// queryString = '{first_ten_companies: company(limit: 10) {id, business_name: name, address, department {department_id: id, name}}}';

const arrayQueryObj = {
  company: [
    {
      _args: {
        limit: 2,
      },
      id: true,
    },
    {
      _alias: 'pagination',
      _args: {
        limit: 1,
      },
      aggregate: true,
    },
  ],
};
const arrayQueryString = buildQueryString( arrayQueryObj );
// arrayQueryString = '{company(limit: 2) {id}, pagination: company(limit: 1) {aggregate}}';

const transformQueryObj = {
  company: {
    companyLogo: {
      _transform: {
        _alias: 'companyLogoSrc',
        _args: {
          transformMakeThumb: 'medium',
        },
      },
    },
  },
};
const transformQueryString = buildQueryString( transformQueryObj );
// transformQueryString = '{company {companyLogoSrc: companyLogo(trahsformMakeThumb: "medium")}}';

buildQueryStringPart(obj, key) → {string}

Build a query string part from a query object and key for use in Hermes query.

Parameters:
NameTypeDescription
objobject

The query object to convert to a string.

keystring

The key of the query object.

Since
  • 4.1.0
Throws:

If transform does not have _alias and _args keys.

Type
Error
Returns:

The query string part.

Type: 
string

buildQueryStringRecursive(obj) → {string}

Recursively build a query string from a query object for use in Hermes query.

Parameters:
NameTypeDescription
objobject | Array

The query object or array to convert to a string.

Since
  • 4.0.5
Throws:

If an array of queries for the same key do not have aliases.

Type
Error
Returns:

The query string.

Type: 
string

buildUpdateObject(obj) → {object}

Build an update object for use in Hermes mutation.

Parameters:
NameTypeDescription
objobject

The object to convert to an update object.

Since
  • 4.1.0
Returns:

The update object.

Type: 
object

removeCurrentFromMenuItem(item)

Remove the current class and attributes from a menu item.

Parameters:
NameTypeDescription
itemHTMLElement

The menu item to remove the current class and attributes from.

Since
  • 4.0.2

stringifyArg(arg) → {string}

Build a string representation of a variable for use in Hermes query.

Parameters:
NameTypeDescription
arg*

The property to convert to a string.

Since
  • 4.0.5
Returns:

The string representation of the property.

Type: 
string

useCache(initialState) → {object}

A hook to manage a cache.

Parameters:
NameTypeDescription
initialStateobject

The initial state of the cache.

Since
  • 4.0.2
Returns:

The cache state and actions.

Type: 
object

useId(defaultId) → {string}

Generates a unique ID if default ID is not provided.

Parameters:
NameTypeDescription
defaultIdstring

The default ID.

Since
  • 3.2.2
Returns:

The ID.

Type: 
string

validateAliases(arr, key) → {boolean}

Validates an array of objects to ensure three conditions related to _alias properties: 1. At least (array length - 1) objects must have an _alias key. 2. All _alias values must be unique. 3. None of the _alias values must match key.

Parameters:
NameTypeDescription
arrArray

The array to validate.

keystring

The key to avoid.

Returns:

Whether the array passes validation or not.

Type: 
boolean