Skip to content

chore: example for setters argument for @rest directive #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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" }
# ]
)
}
41 changes: 41 additions & 0 deletions transforms/setters/api.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# This example illustrates how to map a JSON response to the fields of a GraphQL object.

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

id: ID
name: String
address: Address
}

type Address {
city: String
country: String
state: String
street: String
postalCode: String
}

type Query {
# ecmascript generates customer data to simulate a REST api with a JSON response.
# To verify with a real data source (API or a database) it is required to change the `endpoint` argument on the `@rest` directive.
# https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service
customer(id: ID!): Customer
@rest(
endpoint: "stepzen:empty"
ecmascript: """
function transformREST() {
var id = get('id')
if (id==1)
return ({"location":{"address":{"city":"Raleigh","country":"USA","postalCode":"54321","state":"NC","street":"101 Main St"}},"customerId":"12345","customerName":"John Doe"})
else
return ({"location":{"address":{"city":"Hyderabad","country":"India","postalCode":"654231","state":"TS","street":"J.N.T.U Colony"}},"customerId":"21345","customerName":"Siddarth A"})
}
"""

# mapping from JSON response values to the fields of the GraphQL result.
setters: [
{ field: "id", path: "customerId" } # JSON response field 'customerId' is mapped to GraphQL 'id'
{ field: "name", path: "customerName" } # JSON response field 'customerName' is mapped to GraphQL 'name'
{ field: "address", path: "location.address" } # JSON response nested field 'location.address' mapped to GraphQL 'address'
]
)
}
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
}
12 changes: 12 additions & 0 deletions transforms/setters/requests.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# maps some of JSON response values to the fields of the GraphQL result.
query CustomerByID($id: ID!) {
customer(id: $id) {
address {
city
state
country
}
id
name
}
}
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"
}
52 changes: 52 additions & 0 deletions transforms/setters/tests/Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const fs = require("fs");
const path = require("node:path");
const {
deployAndRun,
stepzen,
getTestDescription,
} = require("../../../tests/gqltest.js");

testDescription = getTestDescription("snippets", __dirname);

const requestsFile = path.join(path.dirname(__dirname), "requests.graphql");
const requests = fs.readFileSync(requestsFile, "utf8").toString();

describe(testDescription, function () {
const tests = [
{
label: "customer-by-id-1",
query: requests,
operationName: "CustomerByID",
variables: { id: 1 },
expected: {
customer: {
address: {
city: "Raleigh",
country: "USA",
state: "NC",
},
id: "12345",
name: "John Doe",
},
},
},
{
label: "customer-by-id-100",
query: requests,
operationName: "CustomerByID",
variables: { id: 100 },
expected: {
customer: {
address: {
city: "Hyderabad",
country: "India",
state: "TS",
},
id: "21345",
name: "Siddarth A",
},
},
},
];
return deployAndRun(__dirname, tests, stepzen.admin);
});