# GraphQL

## Features of this Example

* Nested GraphQL queries
* GraphQL mutations

### Walkthrough

This example was referenced in a walkthrough about [GraphQL Health Checks and Uptime](https://asserted.io/posts/graphql-health-check-uptime)

### Try it out

Repo is available [here](https://github.com/assertedio/node-uptime).

```bash
# Clone example
git clone https://github.com/assertedio/graphql-uptime

# Enter directory and install
cd node-uptime/
npm install

# Run asserted tests
npm run test:asrtd
```

### Tests

Can also be viewed on github [here](https://github.com/assertedio/graphql-uptime/blob/master/.asserted/example.asrtd.js).&#x20;

```javascript
const { expect } = require('chai');
const got = require('got');
const { v4: uuid } = require('uuid');

const client = got.extend({
  prefixUrl: 'http://localhost:4000',
});

const createdBookName = `name-${uuid()}`;

describe('graphql api tests', () => {
  let createBookId;

  it('get all books', async () => {
    const { data } = await client
      .post('', {
        headers: {
          'content-type': 'application/json',
        },
        json: {
          query: `{
          books {
            id
          }
        }`,
        },
      })
      .json();

    expect(data).to.eql({
      books: [
        {
          id: 'design-patterns',
        },
        {
          id: 'refactoring',
        },
        {
          id: 'patterns-of-enterprise-application-architecture',
        },
        {
          id: 'domain-driven-design',
        },
        {
          id: 'clean-code',
        },
        {
          id: 'agile-software-development',
        },
      ],
    });
  });

  it('get other books written by the author', async () => {
    const { data } = await client
      .post('', {
        headers: {
          'content-type': 'application/json',
        },
        json: {
          query: `{
          book(id: "clean-code") {
            name
            authors {
              name
              books {
                name
              }
            }
          }
        }`,
        },
      })
      .json();

    expect(data).to.eql({
      book: {
        name: 'Clean Code - A Handbook of Agile Software Craftsmanship',
        authors: [
          {
            name: 'Robert C. Martin',
            books: [
              {
                name: 'Clean Code - A Handbook of Agile Software Craftsmanship',
              },
              {
                name: 'Agile Software Development, Principles, Patterns, and Practices',
              },
            ],
          },
        ],
      },
    });
  });

  it('create a book', async () => {
    const { data } = await client
      .post('', {
        headers: {
          'content-type': 'application/json',
        },
        json: {
          query: `mutation($newBook: BookInput!) {
            createBook(book: $newBook) {
              id
              name
            }
          }`,
          variables: {
            newBook: {
              name: createdBookName,
              publisherId: 'new-pub',
            },
          },
        },
      })
      .json();

    const { id, name } = data.createBook;
    createBookId = id;

    expect(createBookId).to.exist;
    expect(name).to.eql(createdBookName);
  });

  it('update a book', async () => {
    expect(createBookId).to.exist;
    const { data } = await client
      .post('', {
        headers: {
          'content-type': 'application/json',
        },
        json: {
          query: `mutation($bookId: ID!, $updatedBook: BookInput!) {
            updateBook(bookId: $bookId, book: $updatedBook) {
              id
              name
            }
          }`,
          variables: {
            bookId: createBookId,
            updatedBook: {
              name: 'new-name',
              publisherId: 'new-pub',
            },
          },
        },
      })
      .json();

    const { id, name } = data.updateBook;

    expect(id).to.eql(createBookId);
    expect(name).to.eql('new-name');
  });

  it('remove a book', async () => {
    expect(createBookId).to.exist;
    await client
      .post('', {
        headers: {
          'content-type': 'application/json',
        },
        json: {
          query: `mutation($bookId: ID!) {
            deleteBook(bookId: $bookId)
          }`,
          variables: {
            bookId: createBookId,
          },
        },
      })
      .json();

    const { data } = await client
      .post('', {
        headers: {
          'content-type': 'application/json',
        },
        json: {
          query: `query($bookId: ID!) {
            book(id: $bookId) {
              id
            }
          }`,
          variables: {
            bookId: createBookId,
          },
        },
      })
      .json();

    expect(data.book).to.eql(null);
  });
});
```


---

# 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.asserted.io/examples/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.
