Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
APPLIES TO: All API Management tiers
Configure a resolver to retrieve or set data for a GraphQL field in an object type specified in a GraphQL schema. The schema must be imported to API Management as a GraphQL API.
Note
Currently, this feature isn't available in workspaces.
Currently, API Management supports resolvers that can access the following data sources:
inbound
and backend
policies in the policy execution pipeline. They don't inherit policies from other scopes. For more information, see Policies in API Management.inbound
scope to validate the request before the resolver is invoked. Configure API-scoped policies on the API policies tab for the API.__typename
field, or be altered using the set-body policy to include __typename
.The following steps create a resolver using an HTTP-based data source. The general steps are similar for any resolver that uses a supported data source.
In the Azure portal, navigate to your API Management instance.
In the left menu, select APIs and then the name of your GraphQL API.
On the Schema tab, review the schema for a field in an object type where you want to configure a resolver.
Select a field, and then in the left margin, hover the pointer.
Select + Add Resolver.
On the Create Resolver page:
In the Resolver policy editor, update the http-data-source
policy with child elements for your scenario.
Update the required http-request
element with policies to transform the GraphQL operation to an HTTP request.
Optionally add an http-response
element, and add child policies to transform the HTTP response of the resolver. If the http-response
element isn't specified, the response is returned as a raw string.
Select Create.
The resolver is attached to the field and appears on the Resolvers tab.
List and manage the resolvers for a GraphQL API on the API's Resolvers tab.
On the Resolvers tab:
The Linked column indicates whether the resolver is configured for a field that's currently in the GraphQL schema. If a resolver isn't linked, it can't be invoked.
In the context menu (...) for a resolver, find commands to Clone, Edit, or Delete a resolver. Clone a listed resolver to quickly create a similar resolver that targets a different type and field.
You can create a new resolver by selecting + Create.
When you edit a single resolver, the Edit resolver page opens. You can:
Update the resolver policy and optionally the data source. Changing the data source overwrites the current resolver policy.
Change the type and field that the resolver targets.
Test and debug the resolver's configuration. As you edit the resolver policy, select Run Test to check the output from the data source, which you can validate against the schema. If errors occur, the response includes troubleshooting information.
context.GraphQL
properties are set to the arguments (Arguments
) and parent object (Parent
) for the current resolver execution.context
variable that is passed through the request and response pipeline is augmented with the GraphQL context when used with a GraphQL resolver.The context.GraphQL.parent
is set to the parent object for the current resolver execution. Consider the following partial schema:
type Comment {
id: ID!
owner: string!
content: string!
}
type Blog {
id: ID!
title: string!
content: string!
comments: [Comment]!
comment(id: ID!): Comment
}
type Query {
getBlog(): [Blog]!
getBlog(id: ID!): Blog
}
Also, consider a GraphQL query for all the information for a specific blog:
query {
getBlog(id: 1) {
title
content
comments {
id
owner
content
}
}
}
If you set a resolver for the comments
field in the Blog
type, you'll want to understand which blog ID to use. You can get the ID of the blog using context.GraphQL.Parent["id"]
as shown in the following resolver:
<http-data-source>
<http-request>
<set-method>GET</set-method>
<set-url>@($"https://data.contoso.com/api/blog/{context.GraphQL.Parent["id"]}")
</set-url>
</http-request>
</http-data-source>
The arguments for a parameterized GraphQL query are added to context.GraphQL.Arguments
. For example, consider the following two queries:
query($id: Int) {
getComment(id: $id) {
content
}
}
query {
getComment(id: 2) {
content
}
}
These queries are two ways of calling the getComment
resolver. GraphQL sends the following JSON payload:
{
"query": "query($id: Int) { getComment(id: $id) { content } }",
"variables": { "id": 2 }
}
{
"query": "query { getComment(id: 2) { content } }"
}
You can define the resolver as follows:
<http-data-source>
<http-request>
<set-method>GET</set-method>
<set-url>@($"https://data.contoso.com/api/comment/{context.GraphQL.Arguments["id"]}")</set-url>
</http-request>
</http-data-source>
For more resolver examples, see:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Get started with GraphQL in Microsoft Fabric - Training
Learn how GraphQL in Microsoft Fabric works, the key concepts, and practical examples to help users integrate their applications with GraphQL effectively as part of their solutions.
Certification
Microsoft Certified: Azure Cosmos DB Developer Specialty - Certifications
Write efficient queries, create indexing policies, manage, and provision resources in the SQL API and SDK with Microsoft Azure Cosmos DB.