Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions transforms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ For more on what `transforms` is and how it operates within the custom `@rest` d
- [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)
- [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
- [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.
- [setters](setters) shows how to map a JSON response values to the fields of the GraphQL type.
10 changes: 0 additions & 10 deletions transforms/jsonobjectToJsonarray/api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,10 @@ type Query {
# StepZen takes care of calling jq for each element.
transforms: [
{ pathpattern: ["<>*"], editor: "objectToArray" }
# you could comment this out if you want to try setters instead
{
pathpattern: ["[]*"]
editor: "jq:.[]|{id:.name,fullName:.value.fullName,age:.value.age}"
}
]

# for the second transformation, you can use setters. Uncomment the next line and comment the second pathpattern line above to see how it operates.
# In order to make this to work, change the return type from JSON to [Customer].

# setters: [
# { field: "id", path: "name" }
# { field: "fullName", path: "value.fullName" }
# { field: "age", path: "value.age" }
# ]
)
}
34 changes: 34 additions & 0 deletions transforms/setters/api.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# We first transform the JSON result object from REST API the Graphql structure
# and then map the JSON response to the fields corresponding to the GraphQL type.

type Customer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can we have the GraphQL object have the natural names and the response have "weird" names

cId: ID
cName: String
cAddress: Address
}
type Address {
city: String
countryRegion: String
stateProvince: String
street: String
postalCode: String
}

type Query {
customer(id: ID): [Customer]
@rest(
endpoint: "https://json2api-customers-zlwadjbovq-uc.a.run.app/customers?q=id+eq+$id;"
# The jq expression transform each element of the array.
# StepZen takes care of calling jq for each element.
transforms: [
{
pathpattern: []
editor: """
jq:.[]|{id,name,cAddress:{street, city, stateProvince, countryRegion}}
"""
}
]
# mapping from JSON response values to the fields of the GraphQL result.
setters: [{ field: "cId", path: "id" }, { field: "cName", path: "name" }]
)
}
3 changes: 3 additions & 0 deletions transforms/setters/index.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema @sdl(files: ["api.graphql"]) {
query: Query
}
3 changes: 3 additions & 0 deletions transforms/setters/stepzen.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"endpoint": "api/miscellaneous"
}
11 changes: 11 additions & 0 deletions transforms/setters/tests/Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {
deployAndRun,
getTestDescription,
} = require("../../../tests/gqltest.js");

testDescription = getTestDescription("snippets", __dirname);

describe(testDescription, function () {
const tests = [];
return deployAndRun(__dirname, tests);
});