# GraphQL

**Invoking a GraphQL Query**

In our workflow definition, we can then use a function definition for the `pet` query field as such:

{% code lineNumbers="true" %}

```json
{
  "functions": [
    {
      "name": "get-one-pet",
      "operation": "https://example.com/pets/graphql#query#pet",
      "type": "graphql"
    }
  ]
}
```

{% endcode %}

Note that the `operation` property has the following format for the `graphql` type:

```
<url_to_graphql_endpoint>#<literal "mutation" or "query">#<mutation_or_query_field>
```

In order to invoke this query, we would use the following `functionRef` parameters:

{% code lineNumbers="true" %}

```json
{
  "refName": "get-one-pet",
  "arguments": {
    "id": 42
  },
  "selectionSet": "{ id, name, favoriteTreat { id } }"
}
```

{% endcode %}

Which would return the following result:

{% code lineNumbers="true" %}

```json
{
  "pet": {
    "id": 42,
    "name": "Snuffles",
    "favoriteTreat": {
      "id": 9001
    }
  }
}
```

{% endcode %}

**Invoking a GraphQL Mutation**

Likewise, we would use the following function definition:

{% code lineNumbers="true" %}

```json
{
  "functions": [
    {
      "name": "create-pet",
      "operation": "https://example.com/pets/graphql#mutation#createPet",
      "type": "graphql"
    }
  ]
}
```

{% endcode %}

With the parameters for the `functionRef`:

{% code lineNumbers="true" %}

```json
{
  "refName": "create-pet",
  "arguments": {
    "pet": {
      "id": 43,
      "name":"Sadaharu",
      "favoriteTreatId": 9001
    }
  },
  "selectionSet": "{ id, name, favoriteTreat { id } }"
}
```

{% endcode %}

Which would execute the mutation, creating the object and returning the following data:

{% code lineNumbers="true" %}

```json
{
  "pet": {
    "id": 43,
    "name": "Sadaharu",
    "favoriteTreat": {
      "id": 9001
    }
  }
}
```

{% endcode %}

Note you can include expressions in both `arguments` and `selectionSet`:

{% code lineNumbers="true" %}

```json
{
  "refName": "get-one-pet",
  "arguments": {
    "id": "${ .petId }"
  },
  "selectionSet": "{ id, name, age(useDogYears: ${ .isPetADog }) { dateOfBirth, years } }"
}
```

{% endcode %}

Expressions must be evaluated before executing the operation.

Note that GraphQL Subscriptions are not supported at this time.

For more information about functions, reference the Functions definitions section.


---

# 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.a4b.vn/xflow/developer-guide/workflow-functions/graphql.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.
