-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f45e618
chore: example for setters argument for @rest directive
asararatnakar c3552b2
prettify
asararatnakar 10e1c20
removed the rest call and simulated JSON response as suggested
asararatnakar a2911a4
update comments
asararatnakar 72a1278
minor edits:
asararatnakar a3e0f07
Merge branch 'main' into add_setters
asararatnakar 12ed847
address comments
asararatnakar fe75e1a
change names to look better
asararatnakar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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' | ||
] | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
schema @sdl(files: ["api.graphql"]) { | ||
query: Query | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"endpoint": "api/miscellaneous" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Actually, can we have the GraphQL object have the natural names and the response have "weird" names