> For the complete documentation index, see [llms.txt](https://docs.compass.art/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.compass.art/automations/custom-actions/compass-api.md).

# @compass/api

This package provides functionalities to interact with the Compass API using GraphQL queries and mutations.

### Usage

```javascript
import api from "@compass/api";
```

### API Reference

#### `api.query(document: string, variables?: Record<string, any>)`

Sends a GraphQL query to the Compass API.

* `document` (required, string): The GraphQL document to send.
* `variables` (optional, Record\<string, any>): The variables to send with the query.

Returns a Promise that resolves with the result of the query.

#### `api.mutate(document: string, variables?: Record<string, any>)`

Sends a GraphQL mutation to the Compass API.

* `document` (required, string): The GraphQL document to send.
* `variables` (optional, Record\<string, any>): The variables to send with the mutation.

Returns a Promise that resolves with the result of the mutation.

### Examples

To send a GraphQL query:

```javascript
import api from "@compass/api";

export default async function() {
    const document = `
      query($id: String!) {
        collection(where: { id: $id }) {
          id
          name
        }
      }
    `;

    const variables = {
      id: '1'
    };

    const {data} = await api.query(document, variables);
    log(data);

    return {
        user: data.user,
    }
}
```
