Skip to content

Commit 43dc1ed

Browse files
committed
Added graphql context
1 parent 491ff05 commit 43dc1ed

File tree

1 file changed

+144
-35
lines changed

1 file changed

+144
-35
lines changed

articles/api-management/graphql-policies.md

Lines changed: 144 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,150 @@ The `set-graphql-resolver` policy retrieves or sets data for a GraphQL field in
166166
</set-graphql-resolver>
167167
```
168168

169-
### Examples
169+
### Elements
170+
171+
| Name | Description | Required |
172+
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
173+
| `set-graphql-resolver` | Root element. | Yes |
174+
| `http-data-source` | Configures the HTTP request and optionally the HTTP response that are used to resolve data for the given `parent-type` and `field`. | Yes |
175+
| `http-request` | Specifies a URL and child policies to configure the resolver's HTTP request. Each child element can be specified at most once. | Yes |
176+
| `set-method`| Method of the resolver's HTTP request, configured using the [set-method](api-management-advanced-policies.md#SetRequestMethod) policy. | Yes |
177+
| `set-url` | URL of the resolver's HTTP request. | Yes |
178+
| `set-header` | Header set in the resolver's HTTP request, configured using the [set-header](api-management-transformation-policies.md#SetHTTPheader) policy. | No |
179+
| `set-body` | Body set in the resolver's HTTP request, configured using the [set-body](api-management-transformation-policies.md#SetBody) policy. | No |
180+
| `authentication-certificate` | Client certificate presented in the resolver's HTTP request, configured using the [authentication-certificate](api-management-authentication-policies.md#ClientCertificate) policy. | No |
181+
| `http-response` | Optionally specifies child policies to configure the resolver's HTTP response. If not specified, the response is returned as a raw string. Each child element can be specified at most once. |
182+
| `json-to-xml` | Transforms the resolver's HTTP response using the [json-to-xml](api-management-transformation-policies.md#ConvertJSONtoXML) policy. | No |
183+
| `xml-to-json` | Transforms the resolver's HTTP response using the [xml-to-json](api-management-transformation-policies.md#ConvertJSONtoXML) policy. | No |
184+
| `find-and-replace` | Transforms the resolver's HTTP response using the [find-and-replace](api-management-transformation-policies.md#Findandreplacestringinbody) policy. | No |
185+
186+
187+
### Attributes
188+
189+
| Name | Description | Required | Default |
190+
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------- |
191+
| `parent-type`| An object type in the GraphQL schema. | Yes | N/A |
192+
| `field`| A field of the specified `parent-type` in the GraphQL schema. | Yes | N/A |
193+
194+
> [!NOTE]
195+
> Currently, the values of `parent-type` and `field` aren't validated by this policy. If they aren't valid, the policy is ignored, and the GraphQL query is forwarded to a GraphQL endpoint (if one is configured).
196+
197+
### Usage
198+
199+
This policy can be used in the following policy [sections](./api-management-howto-policies.md#sections) and [scopes](./api-management-howto-policies.md#scopes).
200+
201+
- **Policy sections:** backend
202+
- **Policy scopes:** all scopes
203+
204+
### GraphQL Context
205+
206+
The `context` variable that is passed through the request and response pipeline is augmented with the GraphQL context when used with `<set-graphql-resolver>` policies.
207+
208+
#### ParentResult
209+
210+
The `context.ParentResult` is set to the parent object for the current resolver execution. Consider the following partial schema:
211+
212+
``` graphql
213+
type Comment {
214+
id: ID!
215+
owner: string!
216+
content: string!
217+
}
218+
219+
type Blog {
220+
id: ID!
221+
title: string!
222+
content: string!
223+
comments: [Comment]!
224+
comment(id: ID!): Comment
225+
}
226+
227+
type Query {
228+
getBlog(): [Blog]!
229+
getBlog(id: ID!): Blog
230+
}
231+
```
232+
233+
Also, consider a GraphQL query for all the information for a specific blog:
234+
235+
``` graphql
236+
query {
237+
getBlog(id: 1) {
238+
title
239+
content
240+
comments {
241+
id
242+
owner
243+
content
244+
}
245+
}
246+
}
247+
```
248+
249+
If you set a resolver for `parent-type="Blog" field="comments"`, you will want to understand which blog ID to use. You can get this using `context.ParentResult.AsJObject()["id"].ToString()`. The policy for configuring this resolver would resemble:
250+
251+
``` xml
252+
<set-graphql-resolver parent-type="Blog" field="comments">
253+
<http-data-source>
254+
<http-request>
255+
<set-method>GET</set-method>
256+
<set-url>@{
257+
var blogId = context.ParentResult.AsJObject()["id"].ToString();
258+
return $"https://data.contoso.com/api/blog/{blogId}";
259+
}</set-url>
260+
</http-request>
261+
</http-data-source>
262+
</set-graphql-resolver>
263+
```
264+
265+
### Arguments
266+
267+
The arguments for a parameterized GraphQL query are added to the body of the request. For example, consider the following two queries:
268+
269+
``` graphql
270+
query($id: Int) {
271+
getComment(id: $id) {
272+
content
273+
}
274+
}
275+
276+
query {
277+
getComment(id: 2) {
278+
content
279+
}
280+
}
281+
```
282+
283+
These queries are two ways of calling the `getComment` resolver. GraphQL sends the following JSON payload:
284+
285+
``` json
286+
{
287+
"query": "query($id: Int) { getComment(id: $id) { content } }",
288+
"variables": { "id": 2 }
289+
}
290+
291+
{
292+
"query": "query { getComment(id: 2) { content } }"
293+
}
294+
```
295+
296+
When the resolver is executed, the `arguments` property is added to the body. You can define the resolver as follows:
297+
298+
``` xml
299+
<set-graphql-resolver parent-type="Blog" field="comments">
300+
<http-data-source>
301+
<http-request>
302+
<set-method>GET</set-method>
303+
<set-url>@{
304+
var commentId = context.Request.Body.As<JObject>(true)["arguments"]["id"];
305+
return $"https://data.contoso.com/api/comment/{commentId}";
306+
}</set-url>
307+
</http-request>
308+
</http-data-source>
309+
</set-graphql-resolver>
310+
```
311+
312+
### More examples
170313

171314
### Resolver for GraphQL query
172315

@@ -282,40 +425,6 @@ type User {
282425
</set-graphql-resolver>
283426
```
284427

285-
### Elements
286428

287-
| Name | Description | Required |
288-
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
289-
| `set-graphql-resolver` | Root element. | Yes |
290-
| `http-data-source` | Configures the HTTP request and optionally the HTTP response that are used to resolve data for the given `parent-type` and `field`. | Yes |
291-
| `http-request` | Specifies a URL and child policies to configure the resolver's HTTP request. Each child element can be specified at most once. | Yes |
292-
| `set-method`| Method of the resolver's HTTP request, configured using the [set-method](api-management-advanced-policies.md#SetRequestMethod) policy. | Yes |
293-
| `set-url` | URL of the resolver's HTTP request. | Yes |
294-
| `set-header` | Header set in the resolver's HTTP request, configured using the [set-header](api-management-transformation-policies.md#SetHTTPheader) policy. | No |
295-
| `set-body` | Body set in the resolver's HTTP request, configured using the [set-body](api-management-transformation-policies.md#SetBody) policy. | No |
296-
| `authentication-certificate` | Client certificate presented in the resolver's HTTP request, configured using the [authentication-certificate](api-management-authentication-policies.md#ClientCertificate) policy. | No |
297-
| `http-response` | Optionally specifies child policies to configure the resolver's HTTP response. If not specified, the response is returned as a raw string. Each child element can be specified at most once. |
298-
| `json-to-xml` | Transforms the resolver's HTTP response using the [json-to-xml](api-management-transformation-policies.md#ConvertJSONtoXML) policy. | No |
299-
| `xml-to-json` | Transforms the resolver's HTTP response using the [xml-to-json](api-management-transformation-policies.md#ConvertJSONtoXML) policy. | No |
300-
| `find-and-replace` | Transforms the resolver's HTTP response using the [find-and-replace](api-management-transformation-policies.md#Findandreplacestringinbody) policy. | No |
301-
302-
303-
### Attributes
304-
305-
| Name | Description | Required | Default |
306-
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------- |
307-
| `parent-type`| An object type in the GraphQL schema. | Yes | N/A |
308-
| `field`| A field of the specified `parent-type` in the GraphQL schema. | Yes | N/A |
309-
310-
> [!NOTE]
311-
> Currently, the values of `parent-type` and `field` aren't validated by this policy. If they aren't valid, the policy is ignored, and the GraphQL query is forwarded to a GraphQL endpoint (if one is configured).
312-
313-
### Usage
314-
315-
This policy can be used in the following policy [sections](./api-management-howto-policies.md#sections) and [scopes](./api-management-howto-policies.md#scopes).
316-
317-
- **Policy sections:** backend
318-
319-
- **Policy scopes:** all scopes
320429

321430
[!INCLUDE [api-management-policy-ref-next-steps](../../includes/api-management-policy-ref-next-steps.md)]

0 commit comments

Comments
 (0)