Skip to content

Commit 10e1c20

Browse files
committed
removed the rest call and simulated JSON response as suggested
Signed-off-by: asararatnakar <[email protected]>
1 parent c3552b2 commit 10e1c20

File tree

3 files changed

+87
-19
lines changed

3 files changed

+87
-19
lines changed

transforms/setters/api.graphql

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
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.
1+
# This example demonstartes mapping a JSON response to the fields of the GraphQL.
32

43
type Customer {
54
cId: ID
65
cName: String
76
cAddress: Address
87
}
8+
99
type Address {
1010
city: String
11-
countryRegion: String
12-
stateProvince: String
11+
country: String
12+
state: String
1313
street: String
1414
postalCode: String
1515
}
1616

1717
type Query {
18-
customer(id: ID): [Customer]
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+
customerByID(id: ID!): Customer
1922
@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-
]
23+
endpoint: "stepzen:empty"
24+
ecmascript: """
25+
function transformREST() {
26+
var id = get('id')
27+
if (id==1)
28+
return ({"address":{"city":"Raleigh","country":"USA","postalCode":"54321","state":"NC","street":"101 Main St"},"id":"12345","name":"John Doe"})
29+
else
30+
return ({"address":{"city":"Hyderabad","country":"India","postalCode":"654231","state":"TS","street":"J.N.T.U Colony"},"id":"21345","name":"Siddarth A"})
31+
}
32+
"""
33+
3134
# mapping from JSON response values to the fields of the GraphQL result.
32-
setters: [{ field: "cId", path: "id" }, { field: "cName", path: "name" }]
35+
setters: [
36+
{ field: "cId", path: "id" } # cId mapped to 'id'
37+
{ field: "cName", path: "name" } # cName mapped to 'name'
38+
{ field: "cAddress", path: "address" } # cAddress mapped to 'address'
39+
]
3340
)
3441
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# maps some of JSON response values to the fields of the GraphQL result.
2+
query CustomerByID($id: ID!) {
3+
customerByID(id: $id) {
4+
cAddress {
5+
city
6+
country
7+
state
8+
street
9+
postalCode
10+
}
11+
cId
12+
cName
13+
}
14+
}

transforms/setters/tests/Test.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,58 @@
1+
const fs = require('fs');
2+
const path = require("node:path");
13
const {
24
deployAndRun,
5+
stepzen,
36
getTestDescription,
47
} = require("../../../tests/gqltest.js");
58

69
testDescription = getTestDescription("snippets", __dirname);
710

11+
const requestsFile = path.join(path.dirname(__dirname), 'requests.graphql');
12+
const requests = fs.readFileSync(requestsFile, 'utf8').toString();
13+
814
describe(testDescription, function () {
9-
const tests = [];
10-
return deployAndRun(__dirname, tests);
15+
const tests = [
16+
{
17+
label: "customer-by-id-1",
18+
query: requests,
19+
operationName: 'CustomerByID',
20+
variables: { id: 1 },
21+
expected: {
22+
customerByID: {
23+
cAddress: {
24+
city: "Raleigh",
25+
country: "USA",
26+
state: "NC",
27+
street: "101 Main St",
28+
postalCode: "54321"
29+
},
30+
cId: "12345",
31+
cName: "John Doe"
32+
33+
}
34+
}
35+
},
36+
{
37+
label: "customer-by-id-100",
38+
query: requests,
39+
operationName: 'CustomerByID',
40+
variables: { id: 100 },
41+
expected: {
42+
customerByID: {
43+
cAddress: {
44+
city: "Hyderabad",
45+
country: "India",
46+
state: "TS",
47+
street: "J.N.T.U Colony",
48+
postalCode: "654231"
49+
},
50+
cId: "21345",
51+
cName: "Siddarth A"
52+
53+
}
54+
}
55+
}
56+
]
57+
return deployAndRun(__dirname, tests, stepzen.admin);
1158
});

0 commit comments

Comments
 (0)