Skip to content

Commit f45e618

Browse files
committed
chore: example for setters argument for @rest directive
Signed-off-by: asararatnakar <[email protected]>
1 parent 15b26fb commit f45e618

File tree

6 files changed

+53
-10
lines changed

6 files changed

+53
-10
lines changed

transforms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ For more on what `transforms` is and how it operates within the custom `@rest` d
1010
- [combineintostring](combineintostring) shows how a new field in the return type can be created by concatenating some other fields (like address parts into one address)
1111
- [jsonarrayToJsonobject](jsonarrayToJsonobject) shows how an array of data (say coords) can be converted into an object, `{"lat":, "lon",..}` so that it can be fed into some other system that requires data to be expressed that way
1212
- [jsonobjectToJsonarray](jsonobjectToJsonarray) shows how an object (typically where each key is an id of a record) can be converted into an array (e.g., `{"1":{"name": "john"}, "2": "jane"}` can be converted to `[{"id":"1", "name": "john"}, {"id":"2", name: "jane"}]`), so that it can then behave well for GraphQL processing.
13+
- [setters](setters) shows how to map a JSON response values to the fields of the GraphQL type.

transforms/jsonobjectToJsonarray/api.graphql

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,10 @@ type Query {
5858
# StepZen takes care of calling jq for each element.
5959
transforms: [
6060
{ pathpattern: ["<>*"], editor: "objectToArray" }
61-
# you could comment this out if you want to try setters instead
6261
{
6362
pathpattern: ["[]*"]
6463
editor: "jq:.[]|{id:.name,fullName:.value.fullName,age:.value.age}"
6564
}
6665
]
67-
68-
# for the second transformation, you can use setters. Uncomment the next line and comment the second pathpattern line above to see how it operates.
69-
# In order to make this to work, change the return type from JSON to [Customer].
70-
71-
# setters: [
72-
# { field: "id", path: "name" }
73-
# { field: "fullName", path: "value.fullName" }
74-
# { field: "age", path: "value.age" }
75-
# ]
7666
)
7767
}

transforms/setters/api.graphql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# We first transform the JSON result object from REST API the Graphql structure
2+
# and then map the JSON response to the fields corresponding to the GraphQL type.
3+
4+
type Customer {
5+
cId: ID
6+
cName: String
7+
cAddress: Address
8+
}
9+
type Address {
10+
city: String
11+
countryRegion: String
12+
stateProvince: String
13+
street: String
14+
postalCode: String
15+
}
16+
17+
type Query {
18+
customer(id: ID): [Customer]
19+
@rest(
20+
endpoint: "https://json2api-customers-zlwadjbovq-uc.a.run.app/customers?q=id+eq+$id;"
21+
# The jq expression transform each element of the array.
22+
# StepZen takes care of calling jq for each element.
23+
transforms: [
24+
{
25+
pathpattern: []
26+
editor: """
27+
jq:.[]|{id,name,cAddress:{street, city, stateProvince, countryRegion}}
28+
"""
29+
}
30+
]
31+
# mapping from JSON response values to the fields of the GraphQL result.
32+
setters: [{ field: "cId", path: "id" }, { field: "cName", path: "name" }]
33+
)
34+
}

transforms/setters/index.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
schema @sdl(files: ["api.graphql"]) {
2+
query: Query
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"endpoint": "api/miscellaneous"
3+
}

transforms/setters/tests/Test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const {
2+
deployAndRun,
3+
getTestDescription,
4+
} = require("../../../tests/gqltest.js");
5+
6+
testDescription = getTestDescription("snippets", __dirname);
7+
8+
describe(testDescription, function () {
9+
const tests = [
10+
]
11+
return deployAndRun(__dirname, tests);
12+
});

0 commit comments

Comments
 (0)