title | titleSuffix | description | ms.service | author | ms.author | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|---|---|---|
Import GraphQL schema and set up field resolvers | Microsoft Docs |
Import a GraphQL schema to API Management and configure a policy to resolve a GraphQL query using an HTTP-based data source. |
api-management |
dlepow |
danlep |
how-to |
05/17/2022 |
event-tier1-build-2022 |
[!INCLUDE api-management-graphql-intro.md]
[!INCLUDE preview-callout-graphql.md]
In this article, you'll:
[!div class="checklist"]
- Import a GraphQL schema to your API Management instance
- Set up a resolver for a GraphQL query using an existing HTTP endpoints
- Test your GraphQL API
If you want to expose an existing GraphQL endpoint as an API, see Import a GraphQL API.
- An existing API Management instance. Create one if you haven't already.
- A valid GraphQL schema file with the
.graphql
extension. - A backend GraphQL endpoint is optional for this scenario.
[!INCLUDE api-management-navigate-to-instance.md]
-
From the side navigation menu, under the APIs section, select APIs.
-
Under Define a new API, select the Synthetic GraphQL icon.
:::image type="content" source="media/graphql-schema-resolve-api/import-graphql-api.png" alt-text="Screenshot of selecting Synthetic GraphQL icon from list of APIs.":::
-
In the dialog box, select Full and complete the required form fields.
:::image type="content" source="media/graphql-schema-resolve-api/create-from-graphql-schema.png" alt-text="Screenshot of fields for creating a GraphQL API.":::
Field Description Display name The name by which your GraphQL API will be displayed. Name Raw name of the GraphQL API. Automatically populates as you type the display name. Fallback GraphQL endpoint For this scenario, optionally enter a URL with a GraphQL API endpoint name. API Management passes GraphQL queries to this endpoint when a custom resolver isn't set for a field. Upload schema file Select to browse and upload a valid GraphQL schema file with the .graphql
extension.Description Add a description of your API. URL scheme Select HTTP, HTTPS, or Both. Default selection: Both. API URL suffix Add a URL suffix to identify this specific API in this API Management instance. It has to be unique in this API Management instance. Base URL Uneditable field displaying your API base URL Tags Associate your GraphQL API with new or existing tags. Products Associate your GraphQL API with a product to publish it. Gateways Associate your GraphQL API with existing gateways. Default gateway selection: Managed. Version this API? Select to apply a versioning scheme to your GraphQL API. -
Select Create.
-
After the API is created, browse the schema on the Design tab, in the Frontend section.
Configure the set-graphql-resolver policy to map a field in the schema to an existing HTTP endpoint.
Suppose you imported the following basic GraphQL schema and wanted to set up a resolver for the users query.
type Query {
users: [User]
}
type User {
id: String!
name: String!
}
-
From the side navigation menu, under the APIs section, select APIs > your GraphQL API.
-
On the Design tab of your GraphQL API, select All operations.
-
In the Backend processing section, select + Add policy.
-
Configure the
set-graphql-resolver
policy to resolve the users query using an HTTP data source.For example, the following
set-graphql-resolver
policy retrieves the users field by using aGET
call on an existing HTTP data source.<set-graphql-resolver parent-type="Query" field="users"> <http-data-source> <http-request> <set-method>GET</set-method> <set-url>https://myapi.contoso.com/users</set-url> </http-request> </http-data-source> </set-graphql-resolver>
-
To resolve data for other fields in the schema, repeat the preceding step.
-
Select Save.
[!INCLUDE api-management-graphql-test.md]
[!INCLUDE api-management-define-api-topics.md]
[!div class="nextstepaction"] Transform and protect a published API