Creating a GraphQL API
Creating a GraphQL API by Importing an API from URLCreating a GraphQL API by Importing an API from a FileAPI Gateway provides the ability to view, create, and manage GraphQL APIs, and publish the APIs to a configured destination for consumption. API Gateway supports proxying an existing GraphQL API endpoint and provides API management capabilities to clients like authentication and analytics. This section describes how you can create your first GraphQL API in API Gateway.
Creating GraphQL API in API Gateway
In API Gateway, you can create GraphQL APIs by importing it in two ways:
- Importing an API from URL
- Importing an API from a file
Creating a GraphQL API by Importing an API from URL
Before you begin
Ensure that you have
Manage APIs or Activate/Deactivate APIs functional privilege.
NoteIf you are an Administrator, you would have this privilege.You must have a GraphQL API base URL to create an API using the importer. For example, you can use the Star Wars base URL.
To create a GraphQL API by importing an API from URL
Let’s look at a sample scenario, where you are creating a GraphQL API, Star Wars API, by importing an API from URL.
Click APIs in the title navigation bar.
A list of available APIs appears.Click Create API.
Select Import API from URL.
Type the URL from where the API is imported. For example, use the Star Wars base URL.
Select Protected to make the API a protected API and provide the Username and Password.
Provide a description for the API in the Description field.
Type a name for the API name in the Name field. For example, Starwars_GraphQL_URL.
Select GraphQL SDL from the Type dropdown menu.
Provide a version for the API in the Version field.
From the Team drop-down list, select the teams that you want to assign the API to. For this use case, select Administrator.
Click Create.
A GraphQL API is created with the default Straight Through Routing policy.NoteFor the API to work, you must replace the Endpoint URI of the default Straight Through Routing policy with the query string of the native API.Click Edit to replace the Endpoint URI of the Straight Through Routing policy.
Navigate to Policies > Routing > Straight Through Routing and replace the ${sys:query_string} alias in the Endpoint URI with the query string of the native API.
For example, use the native API endpoint of the Star Wars API,that is, https://swapi-graphql.netlify.app/.netlify/functions/index.Click Activate.
Click Yes in the confirmation dialog box.
The API is now activated. The API Gateway endpoint is now available, and consumers can use this endpoint to invoke the API.
Next steps
- Assess the GraphQL API created is working as expected using Postman. For more information, see Testing GraphQL API using Postman?
- If the GraphQL API is working as expected, you can publish the API to the required destination and expose the API for consumption by the consumers.
Creating a GraphQL API by Importing an API from a File
Before you begin
Ensure that you have
Manage APIs or Activate/Deactivate APIs functional privilege.
NoteIf you are an Administrator, you would have this privilege.You must have a GraphQL SDL (GraphQL Schema Definition Language) file to create an API using the importer. You can download the Starwars.graphql from the Github repository.
To create a GraphQL API by importing an API from a file
Let’s look at a sample scenario, where you are creating a GraphQL API, Star Wars API, by importing an API from from the Starwars.graphql file.
Click APIs in the title navigation bar.
A list of available APIs appears.Click Create API.
Select Import API from file.
Click Browse to select a file.
Select the required file and click Open. For example, choose the Starwars.graphql file that you downloaded from the Github repository.
Type a name for the API name in the Name field. For example, Starwars_GraphQL_file.
Provide a description for the API in the Description field.
Select GraphQL SDL from the Type dropdown menu.
Provide a version for the API in the Version field.
Select the team to which the API must be assigned in the Team field.
Click Create.
A GraphQL API is created with the default Straight Through Routing policy.NoteFor the API to work, you must replace the Endpoint URI of the default Straight Through Routing policy with the query string of the native API.Click Edit to replace the Endpoint URI of the Straight Through Routing policy.
Navigate to Policies > Routing > Straight Through Routing and replace the ${sys:query_string} alias in the Endpoint URI with the query string of the native API.
For example, use the native API endpoint of the Star Wars API, that is, https://swapi-graphql.netlify.app/.netlify/functions/index.Click Activate.
Click Yes in the confirmation dialog box.
The API is now activated. The API Gateway endpoint is now available, and consumers can use this endpoint to invoke the API.
Next steps
- Assess the GraphQL API created is working as expected using Postman. For more information, see Testing GraphQL API using Postman?
- If the GraphQL API is working as expected, you can publish the API to the required destination and expose the API for consumption by the consumers.
Testing a GraphQL API using Postman
No subtopics in thissection
Before you begin
Ensure that you have
- Postman installed in your system, or you can you use the web version of Postman.
- Downloaded the sample Star Wars Postman collection (GraphQL.postman_collection.json) from the Github repository.
You must assess the GraphQL API created is working as expected before exposing it to the API consumers. You should verify the correct status code of the response. For instance, successful HTTP requests should return 200 OK, creating a resource should return 201 CREATED, and so on.
Let’s test the Starwars GraphQL API, which was imported using a file. In this example, to test whether the Star Wars API is created successfully, let’s retrieve all the films in the Star Wars franchise using the Starwars API. The API response should list all the films in the Star Wars franchise.
To test the GraphQL API using Postman
Open Postman.
Click Import.
To Import the sample Star Wars Postman collection (GraphQL.postman_collection.json) downloaded from the Github repository.Select GraphQL and select the GraphQL.postman_collection.json file from your local folder.
Click Import.
Open the imported GraphQL API collections. Select Starwars API GW.
Select a method. For example, select the method as POST.
Specify the request URL. For example, you can provide the Star Wars endpoint from the API you created earlier.
Set the request body type as GraphQL.
Pass the query in the QUERY section. For example, write a query to fetch a list of all the films in the Star Wars franchise.
Click Send.
200 OK HTTP Status is displayed. The response lists all the films in the Star Wars franchise.The API is tested and working as expected. You can now expose the API to the consumers.