---
schema_version: "1"
id: 4f98849d58ee9f4a3ca9ef180618be75b16d704e2384775c6db9c441745d041b
title: Implement a GraphQL API
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/anypoint-code-builder/tut-graphql-implement-api.md
canonical_url: https://docs.mulesoft.com/anypoint-code-builder/tut-graphql-implement-api
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f38ea4b77b77b01c2f7ad149b1d2299cb6d7b186ed5c6e1df4f87a0fb405148c
description: MuleSoft Documentation Site
latest: true
---
---
product: Anypoint Code Builder
version: master
---

# Implement a GraphQL API

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Publish a GraphQL API schema to Anypoint Exchange. Then use Anypoint Code Builder to import and
scaffold the schema into a Mule application. Run and implement the interface.

## Before You Begin

Complete these procedures:

- Set up your MuleSoft environment. See [Getting Started with Anypoint Code Builder](https://docs.mulesoft.com/anypoint-code-builder/start-acb) for
  more information.

- Download the [**Books API** GraphQL schema](https://docs.mulesoft.com/anypoint-code-builder/_attachments/books-api.graphql).

  Alternatively, create a file named `books-api.graphql` with the following content:

  ```graphql
  schema {
    query: Query
  }

  type Query {
    bookById(id: ID): Book
    books: [Book]
    bestsellers: Bestsellers
  }

  type Book {
    id: ID!
    name: String
    pageCount: Int
    author: Author
  }

  type Author {
    id: ID!
    firstName: String
    lastName: String
  }

  type Bestsellers {
    books: [Book]
    authors: [Author]
  }
  ```

## Publish the GraphQL API to Exchange

To implement a GraphQL API, the API must first be manually published to Exchange:

1.  Navigate to Anypoint Exchange, and sign in.

    Show me how

    - US cloud (non-EU host): [Exchange (US)](https://anypoint.mulesoft.com/exchange/)

    - EU cloud (EU host): [Exchange (EU)](https://eu1.anypoint.mulesoft.com/exchange/)

2.  Click **Publish new asset**.

3.  For **Name**, type `books-api`.

4.  For **Asset types**, select **GraphQL API**.

5.  For **Method**, select **Upload a GraphQL API Schema**:

    ![A form for publishing a GraphQL API asset](/mulesoft/current/en-us/anypoint-code-builder/_images/upload-graphql-api-exchange-method-select.png)

6.  For **File upload**, click **Choose file** and browse for your `books-api.graphql` schema file.

7.  For **Lifecycle state**, select **Stable**.

8.  Click **Publish**.

## Implement the GraphQL API Schema

Use Anypoint Code Builder to retrieve the schema from Anypoint Exchange and scaffold the schema into
an interface that you can implement.

1.  Open Anypoint Code Builder.

2.  Open the Command Palette.

    Show me how

    - Use the keyboard shortcuts:

      - Mac: Cmd+Shift+p

      - Windows: Ctrl+Shift+p

    - Select **View** > **Command Palette**.

3.  Provide the following command:

    ```command
    MuleSoft: Implement an API Specification
    ```

4.  If prompted to sign in to Anypoint Platform, click **Allow**, and follow the prompts to sign in.

5.  Configure your project using the following values:

    ![A form for implementing a new API specification](/mulesoft/current/en-us/anypoint-code-builder/_images/implement-api-configure-in-task.png)

    1.  **Project Name**: `books-implementation`

    2.  **Project Location**: click **Browse**, and then select your home directory.

    3.  Search for your **books-api** specification in Exchange by typing `books` and pressing
        Return to display the results.

    4.  In the search results, hover over **books-api**, and click the **Add Asset** button that
        appears:

        ![A table of available API specifications with the Add Asset button highlighted](/mulesoft/current/en-us/anypoint-code-builder/_images/select-books-graphql-api.png)

    5.  Provide **Mule runtime** and **Java** versions.

6.  Click **Create Project**.

7.  If prompted, trust the authors of the API specification.

    Trusting is required before you can work on the project.

    After you trust the authors, Anypoint Code Builder scaffolds your GraphQL API and creates a
    project with an empty flow for each type in your Books API GraphQL schema.

    <table><colgroup><col> <col></colgroup><thead><tr><th>GraphQL Schema</th><th>Mule Flow</th></tr></thead><tbody><tr><td><div><div><div><pre><code data-lang="GraphQL">type Query {
      bookById(id: ID): Book
      books: [Book]
      bestsellers: Bestsellers
    }

    type Book { id: ID! name: String pageCount: Int author: Author }

    type Author { id: ID! firstName: String lastName: String }

    type Bestsellers { books: [Book] authors: [Author]
    }</code></pre></div></div></div></td><td><div><div><div><pre><code data-lang="XML">&lt;flow
    name="Query.bookById"/&gt; &lt;flow name="Query.books"/&gt; &lt;flow
    name="Query.bestsellers"/&gt; &lt;flow name="Book.author"/&gt; &lt;flow
    name="Bestsellers.books"/&gt; &lt;flow
    name="Bestsellers.authors"/&gt;</code></pre></div></div></div></td></tr></tbody></table>

    Each flow contains configurations for the elements `<graphql-router:data-fetcher/>`,
    `<set-payload/>`, and \` <graphql-router:serialize/>\`. The `<graphql-router:data-fetcher>`
    element serves as a trigger for a flow. See
    [Mapping a GraphQL API to Your Data Sources](https://docs.mulesoft.com/apikit/latest/apikit-graphql-api-mapping) for
    more information about data fetchers.

8.  Proceed to [Test Your GraphQL Project](https://docs.mulesoft.com/anypoint-code-builder/tut-graphql-test-first-implementation) to run your
    application in Anypoint Code Builder and post GraphQL queries to your endpoint.

[Test Your GraphQL Project](https://docs.mulesoft.com/anypoint-code-builder/tut-graphql-test-first-implementation)
