# Example Using GraphQL

### Toy Store API with GraphQL: A Hands-on Example

This example showcases a basic GraphQL API for a toy store, demonstrating the schema, queries, and mutations.

#### Schema:

```graphql
type Toy {
  id: ID!
  name: String!
  price: Float!
  category: String!
  inStock: Boolean!
}

type Query {
  toys(category: String): [Toy!]!
  toy(id: ID!): Toy
}

type Mutation {
  createToy(name: String!, price: Float!, category: String!): Toy!
}
```

#### Explanation:

* **Types:**
  * `Toy`: Represents a toy object with properties like `id`, `name`, `price`, `category`, and `inStock`.
  * `Query`: Defines available data retrieval operations.
  * `Mutation`: Defines data manipulation operations (optional, not all APIs have mutations).
* **Query:**
  * `toys(category: String)`: Retrieves a list of toys based on an optional category filter.
  * `toy(id: ID!)`: Fetches a specific toy by its ID.
* **Mutation:**
  * `createToy(name: String!, price: Float!, category: String!)`: Creates a new toy with provided details.

**Query Example (Get all toys):**

```graphql
query {
  toys
}
```

#### Response:

```graphql
{
  "data": {
    "toys": [
      {
        "id": "1",
        "name": "Stuffed Bear",
        "price": 19.99,
        "category": "Plushies",
        "inStock": true
      },
      {
        "id": "2",
        "name": "Remote Control Car",
        "price": 49.99,
        "category": "Vehicles",
        "inStock": false
      }
    ]
  }
}

```

**Query Example (Get a toy by ID):**

```graphql
query getToy($id: ID!) {
  toy(id: $id) {
    id
    name
    price
  }
}
```

**Variables:**

```graphql
{
  "id": "1"
}
```

**Response:**

```graphql
{
  "data": {
    "toy": {
      "id": "1",
      "name": "Stuffed Bear",
      "price": 19.99
    }
  }
}
```

**Mutation Example (Create a new toy):**

```graphql
mutation createToy($name: String!, $price: Float!, $category: String!) {
  createToy(name: $name, price: $price, category: $category) {
    id
    name
    price
    category
  }
}
```

#### Variables:

```graphql
{
  "name": "Building Blocks",
  "price": 24.99,
  "category": "Construction"
}
```

#### Response:

```graphql
{
  "data": {
    "createToy": {
      "id": "3",  // Generated by the server
      "name": "Building Blocks",
      "price": 24.99,
      "category": "Construction"
    }
  }
}
```

This example demonstrates the fundamental concepts of using a GraphQL API. By understanding the schema, you can craft queries to retrieve specific data and potentially use mutations to interact with the data (if supported).


---

# 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.inabit.com/api-reference/introduction-to-graphql/what-is-graphql/example-using-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.
