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]
}
Implement a GraphQL API
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 namedbooks-api.graphql
with the following content:
Publish the GraphQL API to Exchange
To implement a GraphQL API, the API must first be manually published to Exchange:
-
Navigate to Anypoint Exchange, and sign in.
Show me how
-
US cloud (non-EU host): Exchange (US)
-
EU cloud (EU host): Exchange (EU)
-
-
Click Publish new asset.
-
For Name, type
books-api
. -
For Asset types, select GraphQL API.
-
For Method, select Upload a GraphQL API Schema:
-
For File upload, click Choose file and browse for your
books-api.graphql
schema file. -
For Lifecycle state, select Stable.
-
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.
-
Open Anypoint Code Builder.
-
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.
-
-
Provide the following command:
MuleSoft: Implement an API Specification
-
If prompted to sign in to Anypoint Platform, click Allow, and follow the prompts to sign in.
-
Configure your project using the following values:
-
Project Name:
books-implementation
-
Project Location: click Browse, and then select your home directory.
-
Search for your books-api specification in Exchange by typing
books
and pressing Return to display the results. -
In the search results, hover over books-api, and click the Add Asset button that appears:
-
Provide Mule runtime and Java versions.
-
-
Click Create Project.
-
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. -
Proceed to Test Your GraphQL Project to run your application in Anypoint Code Builder and post GraphQL queries to your endpoint.