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.

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 for more information.

  • Download the Books API GraphQL schema.

    Alternatively, create a file named books-api.graphql with the following content:
    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
  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 Schema

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

  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

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

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

  3. Provide the following 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:

    implement api configure in task
    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:

      select books graphql api
    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 from the IDE.

    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.

    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"/>

    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 for more information about data fetchers.

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