Contact Us 1-800-596-4880

Implement a GraphQL API

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Open Beta Release: The cloud IDE is in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

This tutorial guides you through creating a Mule application from scratch, implementing a GraphQL API that you publish to Exchange.

Before You Begin

Complete these procedures:

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.

    Show me how
  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:

    upload graphql api exchange method select
  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

  1. Navigate to Anypoint Code Builder.

  2. Open the Command Palette.

    Show me how
    • Use the keyboard shortcuts:

      • Mac: Cmd+Shift+p

      • Windows: Ctrl+Shift+p

    • In the desktop IDE, select View > Command Palette.

    • In the cloud IDE, click the (menu) icon, and select View > Command Palette.

  3. Provide this command:

    MuleSoft: Implement an API Specification
  4. Select the command.

  5. Configure your project using the following values:

    implement api configure in task
    1 Project Name: books-implementation
    2 Project Location: click Browse, and then select your home directory.
    3 Search for an API specification published in Exchange to implement.
    Start typing books and press Return to display a list of possible results. Click Add Asset for the books-api asset:
    select books graphql api
  6. Click Create Project.

    Anypoint Code Builder scaffolds your GraphQL API and creates an empty flow for each type in your Books API GraphQL schema.

    GraphQL Schema Mule Flow
    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]
    }
    <flow name="Query.bookById"/>
    <flow name="Query.books"/>
    <flow name="Query.bestsellers"/>
    <flow name="Book.author"/>
    <flow name="Bestsellers.books"/>
    <flow name="Bestsellers.authors"/>

    Anypoint Code Builder also creates a <graphql-router:data-fetcher> as sources for each of your Mule flows. See Mapping a GraphQL API to Your Data Sources for more information about data fetchers.

  7. Proceed to Test Your GraphQL Implementation to run your application in Anypoint Code Builder and post GraphQL queries to your endpoint.