# ShopifyGraphql

vendor/ShopifyGraphql.js

## new ShopifyGraphql()

Graphql Library

**Returns**

* `Void`

## ShopifyGraphql.send(query, variables\[, connectionInfo, endpoint])

Grapqhl call to Shopify.

**Parameters**

| Name           | Type                                     | Description                                                                                                                                          |            |
| -------------- | ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- |
| query          | `string`                                 | Graphql query with variables.                                                                                                                        |            |
| variables      | `object`                                 | Object with graphql query variables.                                                                                                                 |            |
| connectionInfo | [ConnectionInfo](#object-connectioninfo) | If you would like to connect to a separate Shopify website that Mesa is not installed on, create a Custom App and include a `connectionInfo` object. | *Optional* |
| endpoint       | `string`                                 | Override the Shopify GraphQL endpoint, for example to call a custom API version. Defaults to 'admin/graphql.json'.                                   | *Optional* |

**Examples**

```javascript
// Query example
const query = `query {
    shop {
      name
      primaryDomain {
        url
        host
      }
    }
   }`;
const rest =  Graphql.send(query, '');
```

**Returns**

* `object` Graphql response.

## ShopifyGraphql.addOrUpdateMetafields(id, metafields)

Adds a metafields to Shopify order. This function will add a metafield id to the payload if metafield already exits.

**Parameters**

| Name       | Type                         | Description          |   |
| ---------- | ---------------------------- | -------------------- | - |
| id         | `string`                     | Shopify Order ID.    |   |
| metafields | `Array.&#60;Metafields&#62;` | Array of metafields. |   |

**Examples**

```javascript
const shopifyOrderId = 1234;
const metafields = [{
       namespace: "example",
       key: "example_key",
       value: "example_value"
   }];
const response =  Graphql.addOrUpdateMetafields(shopifyOrderId, metafields, "order");
Mesa.log.info('Graphql response: ', response);
```

**Returns**

* `object` Graphql response

## ShopifyGraphql.addMetafields(id, metafields, entity)

Adds a metafield to a Shopify entity. This function will throw an error if metafield exists and you do not pass an id.

**Parameters**

| Name       | Type                         | Description          |         |
| ---------- | ---------------------------- | -------------------- | ------- |
| id         | `string`                     | Entity id.           |         |
| metafields | `Array.&#60;Metafields&#62;` | Array of metafields. |         |
| entity     | `string`                     | Entity: Order        | Product |

**Returns**

* `Void`

## ShopifyGraphql.groupMetafieldsByKey(data)

Takes metafield results, returns object keyed by the metafield key

**Parameters**

| Name | Type     | Description |   |
| ---- | -------- | ----------- | - |
| data | `object` |             |   |

**Returns**

* `object` metafields

## ShopifyGraphql.buildShopifyId(entity, id)

Builds Shopfiy Graphql id.

**Parameters**

| Name   | Type     | Description             |   |
| ------ | -------- | ----------------------- | - |
| entity | `string` | Shopfiy graphql entity. |   |
| id     | `string` | Shopify entity id.      |   |

**Returns**

* `string` Shopfiy graphql id

## ShopifyGraphql.extractShopifyId(gid)

Builds Shopfiy Graphql id.

**Parameters**

| Name | Type     | Description                                                         |   |
| ---- | -------- | ------------------------------------------------------------------- | - |
| gid  | `string` | Shopfiy graphql id (ex: gid://shopify/ShippingLabelV2/637424009250) |   |

**Returns**

* `string` Shopfiy id without the gid notation (ex: 637424009250)

## {Object} Metafields()

Metafields

**Properties**

**Returns**

* `Void`

## {Object} ConnectionInfo()

connectionInfo parameter for Shopify calls

**Properties**

**Returns**

* `Void`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.getmesa.com/tools/custom-code/libraries/shopifygraphql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
