Skip to content

Commit 3ace8a1

Browse files
chore: example for setters argument for @rest directive (#68)
* chore: example for setters argument for @rest directive Signed-off-by: asararatnakar <[email protected]> * prettify Signed-off-by: asararatnakar <[email protected]> * removed the rest call and simulated JSON response as suggested Signed-off-by: asararatnakar <[email protected]> * update comments Signed-off-by: asararatnakar <[email protected]> * minor edits: Signed-off-by: asararatnakar <[email protected]> * address comments Signed-off-by: asararatnakar <[email protected]> * change names to look better Signed-off-by: asararatnakar <[email protected]> --------- Signed-off-by: asararatnakar <[email protected]>
1 parent 8b52251 commit 3ace8a1

File tree

7 files changed

+112
-10
lines changed

7 files changed

+112
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This example illustrates how to map a JSON response to the fields of a GraphQL object.
2+
3+
type Customer {
4+
id: ID
5+
name: String
6+
address: Address
7+
}
8+
9+
type Address {
10+
city: String
11+
country: String
12+
state: String
13+
street: String
14+
postalCode: String
15+
}
16+
17+
type Query {
18+
# ecmascript generates customer data to simulate a REST api with a JSON response.
19+
# To verify with a real data source (API or a database) it is required to change the `endpoint` argument on the `@rest` directive.
20+
# https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service
21+
customer(id: ID!): Customer
22+
@rest(
23+
endpoint: "stepzen:empty"
24+
ecmascript: """
25+
function transformREST() {
26+
var id = get('id')
27+
if (id==1)
28+
return ({"location":{"address":{"city":"Raleigh","country":"USA","postalCode":"54321","state":"NC","street":"101 Main St"}},"customerId":"12345","customerName":"John Doe"})
29+
else
30+
return ({"location":{"address":{"city":"Hyderabad","country":"India","postalCode":"654231","state":"TS","street":"J.N.T.U Colony"}},"customerId":"21345","customerName":"Siddarth A"})
31+
}
32+
"""
33+
34+
# mapping from JSON response values to the fields of the GraphQL result.
35+
setters: [
36+
{ field: "id", path: "customerId" } # JSON response field 'customerId' is mapped to GraphQL 'id'
37+
{ field: "name", path: "customerName" } # JSON response field 'customerName' is mapped to GraphQL 'name'
38+
{ field: "address", path: "location.address" } # JSON response nested field 'location.address' mapped to GraphQL 'address'
39+
]
40+
)
41+
}

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+
}

transforms/setters/requests.graphql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# maps some of JSON response values to the fields of the GraphQL result.
2+
query CustomerByID($id: ID!) {
3+
customer(id: $id) {
4+
address {
5+
city
6+
state
7+
country
8+
}
9+
id
10+
name
11+
}
12+
}
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const fs = require("fs");
2+
const path = require("node:path");
3+
const {
4+
deployAndRun,
5+
stepzen,
6+
getTestDescription,
7+
} = require("../../../tests/gqltest.js");
8+
9+
testDescription = getTestDescription("snippets", __dirname);
10+
11+
const requestsFile = path.join(path.dirname(__dirname), "requests.graphql");
12+
const requests = fs.readFileSync(requestsFile, "utf8").toString();
13+
14+
describe(testDescription, function () {
15+
const tests = [
16+
{
17+
label: "customer-by-id-1",
18+
query: requests,
19+
operationName: "CustomerByID",
20+
variables: { id: 1 },
21+
expected: {
22+
customer: {
23+
address: {
24+
city: "Raleigh",
25+
country: "USA",
26+
state: "NC",
27+
},
28+
id: "12345",
29+
name: "John Doe",
30+
},
31+
},
32+
},
33+
{
34+
label: "customer-by-id-100",
35+
query: requests,
36+
operationName: "CustomerByID",
37+
variables: { id: 100 },
38+
expected: {
39+
customer: {
40+
address: {
41+
city: "Hyderabad",
42+
country: "India",
43+
state: "TS",
44+
},
45+
id: "21345",
46+
name: "Siddarth A",
47+
},
48+
},
49+
},
50+
];
51+
return deployAndRun(__dirname, tests, stepzen.admin);
52+
});

0 commit comments

Comments
 (0)