A hook to interact with the Hermes API using template literal syntax. This provides a more intuitive API that closely mirrors the server-side query syntax, making queries easier to write and debug.

Parameters:
NameTypeDescription
propsobject

The props for the hook.

Properties
NameTypeDescription
endpointstring

The Hermes AJAX endpoint.

queryActionstring

The AJAX action for queries.

mutationActionstring

The AJAX action for mutations.

securitystring

The security nonce.

queryKeystring

The key in the request body for queries (default: 'query').

mutationKeystring

The key in the request body for mutations (default: 'mutation').

Since
  • 5.1.0
Returns:

The Hermes state and actions.

Type: 
object
Example
const { query, mutation, isLoading } = useHermesTemplate();

// Query with template literal
const fetchContacts = async () => {
    const result = await query`{
        contact(${ args( { limit: 20 } ) }) {
            id,
            firstName,
            lastName
        }
    }`;
    return result?.data?.data?.contact;
};

// Mutation with template literal
const createContact = async ( firstName, lastName ) => {
    await mutation`{
        insert_contact(objects: [${ { firstName, lastName } }]) {
            returning { id, firstName, lastName }
        }
    }`;
};

Methods

(inner) handleLoadingChange(loading)

Callback to update loading state when requests start/end.

Parameters:
NameTypeDescription
loadingboolean

Whether there are active requests.

Since
  • 5.1.0

(inner) mutation(strings, …values) → {Promise.<object>}

Tagged template function for Hermes mutations. Use backticks to write mutations in GraphQL-like syntax.

Parameters:
NameTypeAttributesDescription
stringsArray

The template literal strings.

values*<repeatable>

The interpolated values.

Since
  • 5.1.0
Returns:

The mutation response.

Type: 
Promise.<object>
Example
// Insert
await mutation`{
    insert_contact(objects: [${ { firstName: 'John', lastName: 'Doe' } }]) {
        returning { id, firstName, lastName }
    }
}`;

// Update
await mutation`{
    update_contact(id: ${ contactId }, firstName: ${ newName }) {
        returning { id, firstName }
    }
}`;

// Delete
await mutation`{
    delete_contact(id: ${ contactId }) {}
}`;

// Connect
await mutation`{
    connect_contact_company(objects: [${ { from: contactId, to: companyId } }]) {}
}`;

(inner) query(strings, …values) → {Promise.<object>}

Tagged template function for Hermes queries. Use backticks to write queries in GraphQL-like syntax.

Parameters:
NameTypeAttributesDescription
stringsArray

The template literal strings.

values*<repeatable>

The interpolated values.

Since
  • 5.1.0
Returns:

The query response.

Type: 
Promise.<object>
Example
const result = await query`{
    contact(limit: ${ limit }, search: ${ searchTerm }) {
        id,
        firstName,
        lastName,
        email { address, isPrimary }
    }
}`;