-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 5 commits
f45e618
c3552b2
10e1c20
a2911a4
72a1278
a3e0f07
12ed847
fe75e1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# This example demonstartes mapping a JSON response to the fields of the GraphQL. | ||
|
||
type Customer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
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 | ||
customerByID(id: ID!): Customer | ||
@rest( | ||
endpoint: "stepzen:empty" | ||
ecmascript: """ | ||
function transformREST() { | ||
var id = get('id') | ||
if (id==1) | ||
return ({"address":{"city":"Raleigh","country":"USA","postalCode":"54321","state":"NC","street":"101 Main St"},"id":"12345","name":"John Doe"}) | ||
else | ||
return ({"address":{"city":"Hyderabad","country":"India","postalCode":"654231","state":"TS","street":"J.N.T.U Colony"},"id":"21345","name":"Siddarth A"}) | ||
} | ||
""" | ||
|
||
# mapping from JSON response values to the fields of the GraphQL result. | ||
setters: [ | ||
{ field: "cId", path: "id" } # JSON response field 'id' is mapped to GraphQL 'cId' | ||
{ field: "cName", path: "name" } # JSON response field 'name' is mapped to GraphQL 'cName' | ||
{ field: "cAddress", path: "address" } # JSON response field 'address' mapped to GraphQL 'cAddress' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add one with a nested path, to show that is supported, e.g.
|
||
] | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
schema @sdl(files: ["api.graphql"]) { | ||
query: Query | ||
} |
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!) { | ||
customerByID(id: $id) { | ||
cAddress { | ||
city | ||
state | ||
country | ||
} | ||
cId | ||
cName | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"endpoint": "api/miscellaneous" | ||
} |
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: { | ||
customerByID: { | ||
cAddress: { | ||
city: "Raleigh", | ||
country: "USA", | ||
state: "NC", | ||
}, | ||
cId: "12345", | ||
cName: "John Doe", | ||
}, | ||
}, | ||
}, | ||
{ | ||
label: "customer-by-id-100", | ||
query: requests, | ||
operationName: "CustomerByID", | ||
variables: { id: 100 }, | ||
expected: { | ||
customerByID: { | ||
cAddress: { | ||
city: "Hyderabad", | ||
country: "India", | ||
state: "TS", | ||
}, | ||
cId: "21345", | ||
cName: "Siddarth A", | ||
}, | ||
}, | ||
}, | ||
]; | ||
return deployAndRun(__dirname, tests, stepzen.admin); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo "demonstartes"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"of a GraphQL object"