diff --git a/transforms/README.md b/transforms/README.md index 527c673..0ad010e 100644 --- a/transforms/README.md +++ b/transforms/README.md @@ -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. \ No newline at end of file diff --git a/transforms/jsonobjectToJsonarray/api.graphql b/transforms/jsonobjectToJsonarray/api.graphql index 7e71d44..f896745 100644 --- a/transforms/jsonobjectToJsonarray/api.graphql +++ b/transforms/jsonobjectToJsonarray/api.graphql @@ -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" } - # ] ) } diff --git a/transforms/setters/api.graphql b/transforms/setters/api.graphql new file mode 100644 index 0000000..53458a6 --- /dev/null +++ b/transforms/setters/api.graphql @@ -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' + ] + ) +} diff --git a/transforms/setters/index.graphql b/transforms/setters/index.graphql new file mode 100644 index 0000000..956ed98 --- /dev/null +++ b/transforms/setters/index.graphql @@ -0,0 +1,3 @@ +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/transforms/setters/requests.graphql b/transforms/setters/requests.graphql new file mode 100644 index 0000000..108e087 --- /dev/null +++ b/transforms/setters/requests.graphql @@ -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 + } +} diff --git a/transforms/setters/stepzen.config.json b/transforms/setters/stepzen.config.json new file mode 100644 index 0000000..af1c0ea --- /dev/null +++ b/transforms/setters/stepzen.config.json @@ -0,0 +1,3 @@ +{ + "endpoint": "api/miscellaneous" +} diff --git a/transforms/setters/tests/Test.js b/transforms/setters/tests/Test.js new file mode 100644 index 0000000..2bfb9ed --- /dev/null +++ b/transforms/setters/tests/Test.js @@ -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); +});