diff --git a/dbquery/pagination/operations.graphql b/dbquery/pagination/operations.graphql index 6f9567d..fc4852c 100644 --- a/dbquery/pagination/operations.graphql +++ b/dbquery/pagination/operations.graphql @@ -1,5 +1,5 @@ # Page through customers from a database connection based pagination -query Customers($first: Int!, $after: String = "") { +query Customers($first: Int!, $after: String = "") { customers(first: $first, after: $after) { edges { node { diff --git a/materializer/nestedfieldselection/api.graphql b/materializer/nestedfieldselection/api.graphql index 63180f1..5409144 100644 --- a/materializer/nestedfieldselection/api.graphql +++ b/materializer/nestedfieldselection/api.graphql @@ -1,11 +1,11 @@ # See preamble in README.md type Document { - docId: String - content(id: String!): Content - @materializer(query: "content", arguments:[{name: "id" argument:"id"}]) - author: Author - lastUpdate: Date + docId: String + content(id: String!): Content + @materializer(query: "content", arguments: [{ name: "id", argument: "id" }]) + author: Author + lastUpdate: Date } type Content @mock { @@ -15,36 +15,47 @@ type Content @mock { } type Query { - document: Document - @rest( - endpoint: "stepzen:empty" - transforms: [{ - editor: """jq: {"author": { "name":"Roy Derks", "country":"Netherlands"}}""" - }] - ) - # Content is exposed here to keep the snippet self-contained, but would otherwise not be exposed as an operation - content(id: String!): Content - @connector(type:"echo") + document: Document + @rest( + endpoint: "stepzen:empty" + transforms: [ + { + editor: """ + jq: {"author": { "name":"Roy Derks", "country":"Netherlands"}} + """ + } + ] + ) + # Content is exposed here to keep the snippet self-contained, but would otherwise not be exposed as an operation + content(id: String!): Content @connector(type: "echo") } type Author { - name: String! - country: String + name: String! + country: String } type Query { book: Book @rest( - endpoint: "stepzen:empty" - transforms: [{ - editor: """jq: {"refId": "27", "name": "Fullstack GraphQL"}""" - }] + endpoint: "stepzen:empty" + transforms: [ + { + editor: """ + jq: {"refId": "27", "name": "Fullstack GraphQL"} + """ + } + ] ) } type Book { refId: String! name: String - author: Author @materializer (query: "document {author}") - content: Content @materializer (query: "document {content}", arguments: {name: "id", field: "refId"}) -} \ No newline at end of file + author: Author @materializer(query: "document {author}") + content: Content + @materializer( + query: "document {content}" + arguments: { name: "id", field: "refId" } + ) +} diff --git a/materializer/nestedfieldselection/index.graphql b/materializer/nestedfieldselection/index.graphql index 458a6cd..956ed98 100644 --- a/materializer/nestedfieldselection/index.graphql +++ b/materializer/nestedfieldselection/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/protection/makeAllPublic/index.graphql b/protection/makeAllPublic/index.graphql index 458a6cd..956ed98 100644 --- a/protection/makeAllPublic/index.graphql +++ b/protection/makeAllPublic/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/protection/makeSomePublic/api.graphql b/protection/makeSomePublic/api.graphql index 107e282..5086938 100644 --- a/protection/makeSomePublic/api.graphql +++ b/protection/makeSomePublic/api.graphql @@ -1,30 +1,32 @@ type Customer { - name: String - city: String + name: String + city: String } type Query { - # An ecmascript generator of customer data. - # Of course, in real life you will call an API or a database. You can do that by changing 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(s) { - var id = get('id') - if (id==1) - return (JSON.stringify({"name":"John Doe","city":"Miami"})) - else - return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"})) - } - """ + # An ecmascript generator of customer data. + # Of course, in real life you will call an API or a database. You can do that by changing 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(s) { + var id = get('id') + if (id==1) + return (JSON.stringify({"name":"John Doe","city":"Miami"})) + else + return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"})) + } + """ ) - customers: [Customer] - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - return JSON.stringify([{"name":"John Doe","city":"Miami"}, - {"name":"Jane Smith","city":"Santa Clara"}]) - } - """ + customers: [Customer] + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + return JSON.stringify([{"name":"John Doe","city":"Miami"}, + {"name":"Jane Smith","city":"Santa Clara"}]) + } + """ ) -} \ No newline at end of file +} diff --git a/protection/makeSomePublic/index.graphql b/protection/makeSomePublic/index.graphql index 458a6cd..956ed98 100644 --- a/protection/makeSomePublic/index.graphql +++ b/protection/makeSomePublic/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/rest/morecomplexpost/api.graphql b/rest/morecomplexpost/api.graphql index 29a013e..629068c 100644 --- a/rest/morecomplexpost/api.graphql +++ b/rest/morecomplexpost/api.graphql @@ -1,16 +1,15 @@ -# Here we create a `postbody` from the query argument +# Here we create a `postbody` from the query argument # using `GetJSON` which will return a JSON item associated # with one of the inputs. # `GetJSON` will return a null value if the city or name was not specified. # `GetJSON` is preferred for an 'Accept' header with an `application/JSON` value. # The syntax is `{{.GetJSON "name-of-the-argument"}}` -# +# # You can also use `Get` which will return a string version -# of the input--where possible--and is well suited for use in +# of the input--where possible--and is well suited for use in # XML, forms, etc. However, `Get` gets more complicated # in terms of careful escaping. Wherever possible, use `GetJSON`. - type Root { args: JSON data: String @@ -26,17 +25,17 @@ type Root { # that uses Go language templates and the custom # stepzen function "Get" and "GetJSON". type Query { - restquery(name:String!, city: String!): Root - @rest (endpoint: "https://httpbingo.org/post", - method: POST, - postbody: """ - {"record": { "name": {{.GetJSON "name"}}, - "city": "{{.GetJSON "city"}}"} } - """ - headers: [ - {name: "Accept", value: "application/json"} - {name: "Content-Type", value: "application/json"} - ] + restquery(name: String!, city: String!): Root + @rest( + endpoint: "https://httpbingo.org/post" + method: POST + postbody: """ + {"record": { "name": {{.GetJSON "name"}}, + "city": "{{.GetJSON "city"}}"} } + """ + headers: [ + { name: "Accept", value: "application/json" } + { name: "Content-Type", value: "application/json" } + ] ) } - diff --git a/rest/morecomplexpost/apidebug.graphql b/rest/morecomplexpost/apidebug.graphql index 74a2fc4..e454873 100644 --- a/rest/morecomplexpost/apidebug.graphql +++ b/rest/morecomplexpost/apidebug.graphql @@ -1,13 +1,13 @@ """ Explore an rest API using JSON as the return value as in `restJSONquery` below, then see what the REST API returned in context of how the StepZen services process the request. - + Try issuing the following with name and/or city removed to see the effects. ``` curl https://ACCOUNTNAME.stepzen.net/api/miscellaneous/__graphql \ --header "Authorization: Apikey $(stepzen whoami --apikey)" \ --header "Content-Type: application/json" \ - --data '{"query": "{restJSONquery(name: \"Joe Smith\" city:\"New York\")}"}' + --data '{"query": "{restJSONquery(name: \"Joe Smith\" city:\"New York\")}"}' ``` StepZen services return diagnostics in the GraphQL response @@ -17,21 +17,22 @@ curl https://ACCOUNTNAME.stepzen.net/api/miscellaneous/__graphql \ --header "Authorization: Apikey $(stepzen whoami --adminkey)" \ --header "Content-Type: application/json" \ --header "StepZen-Debug-Level: 1" \ - --data '{"query": "{restJSONquery(name: \"Joe Smith\" city:\"New York\")}"}' + --data '{"query": "{restJSONquery(name: \"Joe Smith\" city:\"New York\")}"}' ``` """ type Query { - # Set a JSON query return type to avoid any type conflicts that you might have during testing. - # You cannot subselect fields this way. - restJSONquery(name:String, city: String): JSON - @rest (endpoint: "https://httpbingo.org/post", - method: POST, - postbody: """ - {"record": { "name": {{.GetJSON "name"}}, "city": {{.GetJSON "city"}}} } - """ - headers: [ - {name: "Accept", value: "application/json"} - {name: "Content-Type", value: "application/json"} - ], + # Set a JSON query return type to avoid any type conflicts that you might have during testing. + # You cannot subselect fields this way. + restJSONquery(name: String, city: String): JSON + @rest( + endpoint: "https://httpbingo.org/post" + method: POST + postbody: """ + {"record": { "name": {{.GetJSON "name"}}, "city": {{.GetJSON "city"}}} } + """ + headers: [ + { name: "Accept", value: "application/json" } + { name: "Content-Type", value: "application/json" } + ] ) -} \ No newline at end of file +} diff --git a/rest/postbody/api.graphql b/rest/postbody/api.graphql index a86234a..aaf4ae3 100644 --- a/rest/postbody/api.graphql +++ b/rest/postbody/api.graphql @@ -13,7 +13,7 @@ type Root { """ Since we used `application/x-www-form-urlencoded` as a value for our `Content-Type` header, StepZen will -encode the postbody as form-encoded and the httpbingo.org/post will return +encode the postbody as form-encoded and the httpbingo.org/post will return it in form. You'll see that `args` and `data` will be empty, and `json` will be null, when you run the following query. query MyQuery { restquery(name: "Joe Smith", city: "New York") { @@ -26,21 +26,22 @@ query MyQuery { } """ type Query { - restquery(name:String, city: String): Root + restquery(name: String, city: String): Root # Set a JSON query return type to avoid any type conflicts that you might have during testing. # You cannot subselect fields this way. # restquery(name:String, city: String): JSON - @rest (endpoint: "https://httpbingo.org/post", - method: POST, - headers: [{name: "Accept", value:"application/json"}, - {name: "Content-Type", value: "application/x-www-form-urlencoded"}], - + @rest( + endpoint: "https://httpbingo.org/post" + method: POST + headers: [ + { name: "Accept", value: "application/json" } + { name: "Content-Type", value: "application/x-www-form-urlencoded" } + ] ) } - """ -When the 'Accept' header is `application/json`, StepZen will automatically populate the body. +When the 'Accept' header is `application/json`, StepZen will automatically populate the body. Note: if method POST is used, The default for `Content-Type` is `application/json`. Using the sample GraphQL query, data will show the contents of the postbody and json as transformed to JSON. @@ -57,14 +58,14 @@ query MyQuery { } } """ - type Query { - restjsonbodyquery(name:String, city: String): Root + restjsonbodyquery(name: String, city: String): Root # Set a JSON query return type to avoid any type conflicts that you might have during testing. # You cannot subselect fields this way. # restjsonbodyquery(name:String, city: String): JSON - @rest (endpoint: "https://httpbingo.org/post", - method: POST, - headers: [{name: "Accept", value:"application/json"}] + @rest( + endpoint: "https://httpbingo.org/post" + method: POST + headers: [{ name: "Accept", value: "application/json" }] ) -} \ No newline at end of file +} diff --git a/rest/postbody/index.graphql b/rest/postbody/index.graphql index 458a6cd..956ed98 100644 --- a/rest/postbody/index.graphql +++ b/rest/postbody/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/rest/restWithConfigYaml/api.graphql b/rest/restWithConfigYaml/api.graphql index 63dca70..843e492 100644 --- a/rest/restWithConfigYaml/api.graphql +++ b/rest/restWithConfigYaml/api.graphql @@ -23,15 +23,15 @@ curl https://ACCOUNTNAME.stepzen.net/api/miscellaneous/__graphql \ --header "Content-Type: application/json" \ --data '{"query": "{rest(q: [\"Joe Smith\" \"Jane Smith\"]) { args { q } url }}"}' """ - type Query { - rest(q:[String]): Root + rest(q: [String]): Root # Set a JSON query return type to avoid any type conflicts that you might have during testing. # You cannot subselect fields this way. # rest(q:[String]): JSON - @rest (endpoint: "https://httpbingo.org/get?apikey=$apikey", - method: GET, - headers: [{name: "Accept", value:"application/json"}], - configuration: "httpbin" + @rest( + endpoint: "https://httpbingo.org/get?apikey=$apikey" + method: GET + headers: [{ name: "Accept", value: "application/json" }] + configuration: "httpbin" ) -} \ No newline at end of file +} diff --git a/rest/restWithConfigYaml/index.graphql b/rest/restWithConfigYaml/index.graphql index 458a6cd..956ed98 100644 --- a/rest/restWithConfigYaml/index.graphql +++ b/rest/restWithConfigYaml/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/rest/restWithParameters/index.graphql b/rest/restWithParameters/index.graphql index 458a6cd..956ed98 100644 --- a/rest/restWithParameters/index.graphql +++ b/rest/restWithParameters/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/routing/rerank/index.graphql b/routing/rerank/index.graphql index 4cc25c6..e405ed8 100644 --- a/routing/rerank/index.graphql +++ b/routing/rerank/index.graphql @@ -3,102 +3,118 @@ Interface JSONDocs represents a type that contains a single JSON field. """ interface JSONDocs { - docs: JSON + docs: JSON } """ Type Concrete implements JSONDocs """ type Concrete implements JSONDocs { - docs: JSON + docs: JSON } type Query { """ createAndSort is the main field that creates a Concrete `JSON` document that is an array of elements, each ranked in ascending, descending or random order depending on the `type` argument. """ - createAndSort (count: Int!, type: String): JSONDocs - @sequence (steps: [ - {query: "createDocs"}, - {query: "reRank", arguments: {name: "type", argument: "type"}} - ]) + createAndSort(count: Int!, type: String): JSONDocs + @sequence( + steps: [ + { query: "createDocs" } + { query: "reRank", arguments: { name: "type", argument: "type" } } + ] + ) """ createDocs computes a JSON document that is an array of `count` elements with randomly generated `distance`. """ - createDocs (count: Int!): Concrete - @rest (endpoint: "stepzen:empty", - ecmascript: """ - function transformREST (s) { - var count = get('count') - var output = [] - var element = {} + createDocs(count: Int!): Concrete + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST (s) { + var count = get('count') + var output = [] + var element = {} - var seed = 1; - function random() { - var x = Math.sin(seed++) * 10000; - return x - Math.floor(x); - } - for (let i = 0; i < count; i++) { - element = {"distance": random(), "content": i.toString()} - output.push (element) - } - return {docs: output} - - } - """) + var seed = 1; + function random() { + var x = Math.sin(seed++) * 10000; + return x - Math.floor(x); + } + for (let i = 0; i < count; i++) { + element = {"distance": random(), "content": i.toString()} + output.push (element) + } + return {docs: output} + + } + """ + ) """ reRank is a field that reorders the elements in `docs` according to the requested `type` argument. This fields is implemented by other fields that supply it based on the value of `type`. - """ - reRank (docs: JSON, type: String): JSONDocs + """ + reRank(docs: JSON, type: String): JSONDocs """ reRankAsc is a field that reorders the elements in `docs` in ascending order of their `distance` field. - """ - reRankAsc ( docs: JSON, type: String): Concrete - @supplies (query: "reRank", if: {src: "type === 'asc'"}) - @rest (endpoint: "stepzen:empty", - transforms: [{ - editor: """jsonata: - ( - $docs := $get("docs"); - { - "docs": $docs^(distance) - } - ) - """ - }] + reRankDesc(docs: JSON, type: String): Concrete + @supplies(query: "reRank", if: { src: "type === 'desc'" }) + @rest( + endpoint: "stepzen:empty" + transforms: [ + { + editor: """ + jsonata: + ( + $docs := $get("docs"); + { + "docs": $docs^(>distance) + } + ) + """ + } + ] ) """ reRankNoop is a field that skips reordering """ - reRankNoop (docs: JSON, type: String): Concrete - @supplies (query: "reRank", if: {src: "type === 'noop'"}) - @rest (endpoint: "stepzen:empty", - transforms: [{ - editor: """jsonata: - ( - $docs := $get("docs"); - { - "docs": $docs - } - ) - """ - }] + reRankNoop(docs: JSON, type: String): Concrete + @supplies(query: "reRank", if: { src: "type === 'noop'" }) + @rest( + endpoint: "stepzen:empty" + transforms: [ + { + editor: """ + jsonata: + ( + $docs := $get("docs"); + { + "docs": $docs + } + ) + """ + } + ] ) -} \ No newline at end of file +} diff --git a/routing/supplies/index.graphql b/routing/supplies/index.graphql index 6dfcd60..eaaab62 100644 --- a/routing/supplies/index.graphql +++ b/routing/supplies/index.graphql @@ -84,7 +84,7 @@ type Query { tyd(id: ID!): ToYourDoor @supplies( query: "expected" - if: {src: "$contains(id, /^TYD-/)", language: JSONATA} + if: { src: "$contains(id, /^TYD-/)", language: JSONATA } ) @rest( endpoint: "stepzen:empty" diff --git a/sequence/arguments/api.graphql b/sequence/arguments/api.graphql index 242f6fd..063605c 100644 --- a/sequence/arguments/api.graphql +++ b/sequence/arguments/api.graphql @@ -12,81 +12,87 @@ # We provide both examples below type Weather { - temp: Float + temp: Float } type Coord { - lat: String - lon: String + lat: String + lon: String } type Customer { - name: String - city: String + name: String + city: String } type Query { + # Queries `weather`,`convertCityOrLocaleToLatLon`, and `weatherByCityOrLocale` use an ecmascript return of weather by lat/lon and units (imperial or metric) + # which is included in the `@rest` directive. + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. + # We are picturing a real-life scenario in which you call a weather API using the `endpoint` argument. + # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - # Queries `weather`,`convertCityOrLocaleToLatLon`, and `weatherByCityOrLocale` use an ecmascript return of weather by lat/lon and units (imperial or metric) - # which is included in the `@rest` directive. - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. - # We are picturing a real-life scenario in which you call a weather API using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. + weather(lat: String, lon: String, units: String): Weather + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var lat = get('lat') + var lon = get('lon') + var units = get('units') + var temp + if (lat==25.76 && lon==-80.19) + temp = 100.0 + else + temp = 60.4 + if (units=='metric') + temp = (temp-32)*5/9 + return (JSON.stringify({temp:temp})) + } + """ + ) + convertCityOrLocaleToLatLon(cityOrLocale: String): Coord + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var city = get('cityOrLocale') + if (city == 'Miami') + return (JSON.stringify({lat:25.76,lon:-80.19})) + else + return (JSON.stringify({lat:37.2,lon:-121.19})) + } + """ + ) + # Below is a sequence call that sequences `convertCityOrLocaleToLatLong` and `weather`. + # Now the `cityOrLocale` argument can fetch weather data by lat/lon. + # In the first step, the arguments are assigned directly from the sequence field's arguments. + # We have to use an `arguments` setting for the second step. + # but we need `units` in the second step. + # The argument named `units` in the `weather` step + # gets its value from the argument `units` in the sequence field + # The second step receives the arguments passed down to it, + # and it also receives the output of the first step by default. + # In this case, the first step returns a type `Coord`, and therefore `Coord`'s' fields `lat` and `lon` are + # available to the second step `weather`. + # For more information including examples, see https://stepzen.com/docs/features/sequencing-queries - weather (lat: String, lon: String, units: String): Weather - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var lat = get('lat') - var lon = get('lon') - var units = get('units') - var temp - if (lat==25.76 && lon==-80.19) - temp = 100.0 - else - temp = 60.4 - if (units=='metric') - temp = (temp-32)*5/9 - return (JSON.stringify({temp:temp})) - } - """ - ) - convertCityOrLocaleToLatLon (cityOrLocale: String):Coord - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var city = get('cityOrLocale') - if (city == 'Miami') - return (JSON.stringify({lat:25.76,lon:-80.19})) - else - return (JSON.stringify({lat:37.2,lon:-121.19})) - } - """ - ) - # Below is a sequence call that sequences `convertCityOrLocaleToLatLong` and `weather`. - # Now the `cityOrLocale` argument can fetch weather data by lat/lon. - # In the first step, the arguments are assigned directly from the sequence field's arguments. - # We have to use an `arguments` setting for the second step. - # but we need `units` in the second step. - # The argument named `units` in the `weather` step - # gets its value from the argument `units` in the sequence field - # The second step receives the arguments passed down to it, - # and it also receives the output of the first step by default. - # In this case, the first step returns a type `Coord`, and therefore `Coord`'s' fields `lat` and `lon` are - # available to the second step `weather`. - # For more information including examples, see https://stepzen.com/docs/features/sequencing-queries - - weatherByCityOrLocale (cityOrLocale: String, units: String): Weather - @sequence(steps:[{query: "convertCityOrLocaleToLatLon"}, - {query: "weather", arguments: [{name: "units", argument: "units"}]}]) - customer (id: ID): Customer - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var id = get('id') - if (id==1) - return (JSON.stringify({name:'John Doe',city:'Miami'})) - else - return (JSON.stringify({name:'Jane Smith',city:'Santa Clara'})) - } - """ + weatherByCityOrLocale(cityOrLocale: String, units: String): Weather + @sequence( + steps: [ + { query: "convertCityOrLocaleToLatLon" } + { query: "weather", arguments: [{ name: "units", argument: "units" }] } + ] + ) + customer(id: ID): Customer + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var id = get('id') + if (id==1) + return (JSON.stringify({name:'John Doe',city:'Miami'})) + else + return (JSON.stringify({name:'Jane Smith',city:'Santa Clara'})) + } + """ ) } @@ -99,8 +105,12 @@ type Query { # Second, because units is not a field in the enclosing type, it needs to be added in the `arguments` argument. extend type Customer { - weather (units: String): Weather - @materializer (query: "weatherByCityOrLocale", arguments: [ - {name: "cityOrLocale", field: "city"} - {name: "units", argument: "units"}]) + weather(units: String): Weather + @materializer( + query: "weatherByCityOrLocale" + arguments: [ + { name: "cityOrLocale", field: "city" } + { name: "units", argument: "units" } + ] + ) } diff --git a/sequence/arguments/index.graphql b/sequence/arguments/index.graphql index 458a6cd..956ed98 100644 --- a/sequence/arguments/index.graphql +++ b/sequence/arguments/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/sequence/forLoops/api.graphql b/sequence/forLoops/api.graphql index ca96d1c..8b68865 100644 --- a/sequence/forLoops/api.graphql +++ b/sequence/forLoops/api.graphql @@ -7,58 +7,59 @@ # The second query gets order details for each of these ids type Customer { - name: String - city: String - orderList: [ID] + name: String + city: String + orderList: [ID] } type Order { - id: ID - product: ID + id: ID + product: ID } type Query { + # Queries `customer`,`orderDetail`, and `echo` use an ecmascript return. + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. + # We are picturing a real-life scenario in which you call a customer datasource using the `endpoint` argument. + # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - # Queries `customer`,`orderDetail`, and `echo` use an ecmascript return. - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. - # We are picturing a real-life scenario in which you call a customer datasource using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - - customer (id: ID): Customer - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var id = get('id') - if (id==1) - return (JSON.stringify({"name":"John Doe","city":"Miami", "orderList":[1,3]})) - else - return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara", "orderList":[2,4]})) - } - """ + customer(id: ID): Customer + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var id = get('id') + if (id==1) + return (JSON.stringify({"name":"John Doe","city":"Miami", "orderList":[1,3]})) + else + return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara", "orderList":[2,4]})) + } + """ ) - orderDetail(id: ID): Order - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var id = get('id') - return (JSON.stringify({"id":id, "product":id+10})) - } - """ + orderDetail(id: ID): Order + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var id = get('id') + return (JSON.stringify({"id":id, "product":id+10})) + } + """ ) - # This is a sequence. The first query in the sequence is an echo, generating a 'for loop'. - # The second query gets called for each entry in the 'for loop'. - - allOrders(ids: [ID]): [Order] - @sequence (steps:[{query: "echo"}, {query: "orderDetail"}]) + # This is a sequence. The first query in the sequence is an echo, generating a 'for loop'. + # The second query gets called for each entry in the 'for loop'. + allOrders(ids: [ID]): [Order] + @sequence(steps: [{ query: "echo" }, { query: "orderDetail" }]) - echo(ids:[ID]):[ID] - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s){ - var ids = get('ids') - return ids - } - """ - ) + echo(ids: [ID]): [ID] + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s){ + var ids = get('ids') + return ids + } + """ + ) } # Now that we have a query that can generate order details for all order ids in the list, @@ -66,7 +67,9 @@ type Query { # We need to map the argument, as shown below. extend type Customer { - orderDetails: [Order] - @materializer (query: "allOrders", arguments:[{name:"ids", field:"orderList"}]) + orderDetails: [Order] + @materializer( + query: "allOrders" + arguments: [{ name: "ids", field: "orderList" }] + ) } - diff --git a/sequence/forLoops/index.graphql b/sequence/forLoops/index.graphql index 458a6cd..956ed98 100644 --- a/sequence/forLoops/index.graphql +++ b/sequence/forLoops/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/sequence/transformsInMaterializer/api.graphql b/sequence/transformsInMaterializer/api.graphql index 29abe25..7b2976d 100644 --- a/sequence/transformsInMaterializer/api.graphql +++ b/sequence/transformsInMaterializer/api.graphql @@ -7,67 +7,69 @@ # We use an intermediate type `Coord` to connect the two. type Weather { - temp: Float + temp: Float } type Coord { - lat: String - lon: String + lat: String + lon: String } type Customer { - name: String - city: String + name: String + city: String } type Query { - # Queries `weather`,`convertCityToLatLon`, and `customer` use an ecmascript return. - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. - # We are picturing a real-life scenario in which you call a weather API and a customer datasource using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - weather (lat: String, lon: String): Weather - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var lat = get('lat') - var lon = get('lon') - if (lat=='25.76' && lon=='-80.19') - return (JSON.stringify({"temp":100.0})) - else - return (JSON.stringify({"temp":60.4})) - } - """ - ) - convertCityToLatLon (city: String):Coord - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var city = get('city') - if (city == 'Miami') - return (JSON.stringify({"lat":"25.76","lon":"-80.19"})) - else - return (JSON.stringify({"lat":"37.2","lon":"-121.19"})) - } - """ - ) - # A sequence call that sequences the queries `converCityToLatLong` and `weather`. - # This enables a `city` argument to be used to fetch weather data by lat/lon. - weatherByCity (city: String): Weather - @sequence(steps:[{query: "convertCityToLatLon"}, {query: "weather"}]) + # Queries `weather`,`convertCityToLatLon`, and `customer` use an ecmascript return. + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. + # We are picturing a real-life scenario in which you call a weather API and a customer datasource using the `endpoint` argument. + # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. + weather(lat: String, lon: String): Weather + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var lat = get('lat') + var lon = get('lon') + if (lat=='25.76' && lon=='-80.19') + return (JSON.stringify({"temp":100.0})) + else + return (JSON.stringify({"temp":60.4})) + } + """ + ) + convertCityToLatLon(city: String): Coord + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var city = get('city') + if (city == 'Miami') + return (JSON.stringify({"lat":"25.76","lon":"-80.19"})) + else + return (JSON.stringify({"lat":"37.2","lon":"-121.19"})) + } + """ + ) + # A sequence call that sequences the queries `converCityToLatLong` and `weather`. + # This enables a `city` argument to be used to fetch weather data by lat/lon. + weatherByCity(city: String): Weather + @sequence(steps: [{ query: "convertCityToLatLon" }, { query: "weather" }]) - customer (id: ID): Customer - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var id = get('id') - if (id==1) - return (JSON.stringify({"name":"John Doe","city":"Miami"})) - else - return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"})) - } - """ + customer(id: ID): Customer + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var id = get('id') + if (id==1) + return (JSON.stringify({"name":"John Doe","city":"Miami"})) + else + return (JSON.stringify({"name":"Jane Smith","city":"Santa Clara"})) + } + """ ) } # As a final step, use @materializer to connect the customer graph with the weather graph. extend type Customer { - weather: Weather - @materializer (query: "weatherByCity") -} \ No newline at end of file + weather: Weather @materializer(query: "weatherByCity") +} diff --git a/sequence/transformsInMaterializer/index.graphql b/sequence/transformsInMaterializer/index.graphql index 458a6cd..956ed98 100644 --- a/sequence/transformsInMaterializer/index.graphql +++ b/sequence/transformsInMaterializer/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/sequence/useOfJSON/api.graphql b/sequence/useOfJSON/api.graphql index cce88a0..30e0001 100644 --- a/sequence/useOfJSON/api.graphql +++ b/sequence/useOfJSON/api.graphql @@ -7,48 +7,54 @@ # Keeping your type system simple means using JSON as the return type of all but the last step. type Weather { - temp: Float + temp: Float } type Query { - # Queries `weatherByJSON`,`convertCityOrLocaleToLatLon`, and `weatherByCityOrLocale` use an ecmascript return, - # which is included in the `@rest` directive. - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. - # We are picturing a real-life scenario in which you call a weather API using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - weatherByJSON (input: JSON): Weather - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var input = get('input') - var lat = input['lat'] - var lon = input['lon'] - var temp - if (lat=='25.76' && lon=='-80.19') - temp = 100.0 - else - temp = 60.4 - return (JSON.stringify({"temp":temp})) - } - """ - ) - # An ecmascript converter from `cityOrLocale` to its lat/lon. - # Here, since this will be called first, we declare it to return JSON. - convertCityOrLocaleToLatLon (cityOrLocale: String):JSON - @rest (endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - var city = get('city') - if (city == 'Miami') - return (JSON.stringify({"lat":"25.76","lon":"-80.19"})) - else - return (JSON.stringify({"lat":"37.2","lon":"-121.19"})) - } - """ - ) - # A `@sequence` directive that sequences `converCityOrLocaleToLatLong` and weather. - # Now, a cityOrLocale argument can be used to fetch weather data by lat/lon. - weatherByCityOrLocale (cityOrLocale: String): Weather - @sequence(steps:[{query: "convertCityOrLocaleToLatLon"}, - {query: "weatherByJSON"}]) -} \ No newline at end of file + # Queries `weatherByJSON`,`convertCityOrLocaleToLatLon`, and `weatherByCityOrLocale` use an ecmascript return, + # which is included in the `@rest` directive. + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. + # We are picturing a real-life scenario in which you call a weather API using the `endpoint` argument. + # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. + weatherByJSON(input: JSON): Weather + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var input = get('input') + var lat = input['lat'] + var lon = input['lon'] + var temp + if (lat=='25.76' && lon=='-80.19') + temp = 100.0 + else + temp = 60.4 + return (JSON.stringify({"temp":temp})) + } + """ + ) + # An ecmascript converter from `cityOrLocale` to its lat/lon. + # Here, since this will be called first, we declare it to return JSON. + convertCityOrLocaleToLatLon(cityOrLocale: String): JSON + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + var city = get('city') + if (city == 'Miami') + return (JSON.stringify({"lat":"25.76","lon":"-80.19"})) + else + return (JSON.stringify({"lat":"37.2","lon":"-121.19"})) + } + """ + ) + # A `@sequence` directive that sequences `converCityOrLocaleToLatLong` and weather. + # Now, a cityOrLocale argument can be used to fetch weather data by lat/lon. + weatherByCityOrLocale(cityOrLocale: String): Weather + @sequence( + steps: [ + { query: "convertCityOrLocaleToLatLon" } + { query: "weatherByJSON" } + ] + ) +} diff --git a/sequence/useOfJSON/index.graphql b/sequence/useOfJSON/index.graphql index 458a6cd..956ed98 100644 --- a/sequence/useOfJSON/index.graphql +++ b/sequence/useOfJSON/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/transforms/combineIntoString/api.graphql b/transforms/combineIntoString/api.graphql index 2d978d5..4e2d311 100644 --- a/transforms/combineIntoString/api.graphql +++ b/transforms/combineIntoString/api.graphql @@ -1,32 +1,36 @@ # Some data might be returned as separate strings, lik "street", "city", "state" etc. # You might want to convert it into one string, either as final output -# or to feed it into a sequence that requires strings (for example, google APIs expect the entire address as a string). +# or to feed it into a sequence that requires strings (for example, google APIs expect the entire address as a string). type Customer { - name: String - address: String + name: String + address: String } - # Queries `customer` uses an ecmascript return, - # which is included in the `@rest` directive. - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. - # We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - +# Queries `customer` uses an ecmascript return, +# which is included in the `@rest` directive. +# See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. +# We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. +# See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. + type Query { - customer: Customer - @rest ( - endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - return(JSON.stringify({"name":"John Doe", "street": "111 Main Street", "city": "Miami", "state": "Florida", "country": "US"})) + customer: Customer + @rest( + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + return(JSON.stringify({"name":"John Doe", "street": "111 Main Street", "city": "Miami", "state": "Florida", "country": "US"})) + } + """ + # Transform it using jq. + # Here the empty `pathpattern` argument means that the jq expression is applied at the root. + transforms: [ + { + pathpattern: [] + editor: """ + jq:.|{name,address:(.street+", "+.city+", "+.state+", "+.country)} + """ } - """ - # Transform it using jq. - # Here the empty `pathpattern` argument means that the jq expression is applied at the root. - transforms: [{pathpattern: [], editor: """ - jq:.|{name,address:(.street+", "+.city+", "+.state+", "+.country)} - """ - }] + ] ) -} \ No newline at end of file +} diff --git a/transforms/combineIntoString/index.graphql b/transforms/combineIntoString/index.graphql index 458a6cd..956ed98 100644 --- a/transforms/combineIntoString/index.graphql +++ b/transforms/combineIntoString/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/transforms/filter/api.graphql b/transforms/filter/api.graphql index 9a82a30..5a415a1 100644 --- a/transforms/filter/api.graphql +++ b/transforms/filter/api.graphql @@ -1,26 +1,27 @@ - # The `ecmascript` argument is great for writing filters. - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. - # We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. +# The `ecmascript` argument is great for writing filters. +# See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. +# We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. +# See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. type Customer { - id: ID - name: String + id: ID + name: String } type Query { - customer(name: String): [Customer] - @rest (endpoint: "https://json2api-customers-zlwadjbovq-uc.a.run.app/customers", - ecmascript: """ - function transformREST(s) { - var name = get('name') - var data = JSON.parse(s) - var output = [] - for (var i=0; i < data.length; i++) { - if (data[i]['name']==name) - output.push(data[i]) - } - return JSON.stringify(output) - } - """ + customer(name: String): [Customer] + @rest( + endpoint: "https://json2api-customers-zlwadjbovq-uc.a.run.app/customers" + ecmascript: """ + function transformREST(s) { + var name = get('name') + var data = JSON.parse(s) + var output = [] + for (var i=0; i < data.length; i++) { + if (data[i]['name']==name) + output.push(data[i]) + } + return JSON.stringify(output) + } + """ ) -} \ No newline at end of file +} diff --git a/transforms/filter/index.graphql b/transforms/filter/index.graphql index 458a6cd..956ed98 100644 --- a/transforms/filter/index.graphql +++ b/transforms/filter/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/transforms/jsonarrayToJsonobject/api.graphql b/transforms/jsonarrayToJsonobject/api.graphql index c869ccf..419e2eb 100644 --- a/transforms/jsonarrayToJsonobject/api.graphql +++ b/transforms/jsonarrayToJsonobject/api.graphql @@ -8,44 +8,51 @@ # Then you can convert it to a pre-existing type. type Coord { - lat: Float - lon: Float + lat: Float + lon: Float } type Query { - coord: Coord - @rest ( - - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on using `ecmascript`. - # We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - return(JSON.stringify([37.7, -121.7])) + coord: Coord + @rest( + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on using `ecmascript`. + # We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. + # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + return(JSON.stringify([37.7, -121.7])) + } + """ + # Next, transform it using jq + # Here the empty pathpattern means that the jq expression is applied at the root. + transforms: [ + { + pathpattern: [] + editor: """ + jq:{lat:.[0],lon:.[1]} + """ } - """ - # Next, transform it using jq - # Here the empty pathpattern means that the jq expression is applied at the root. - transforms: [{pathpattern: [], editor: """ - jq:{lat:.[0],lon:.[1]} - """ - }] + ] ) - deepCoord: JSON - @rest ( - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on using `ecmascript`. - endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - return(JSON.stringify({"address":{"city":"Santa Clara","location":[37.7, -121.7]}})) + deepCoord: JSON + @rest( + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on using `ecmascript`. + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + return(JSON.stringify({"address":{"city":"Santa Clara","location":[37.7, -121.7]}})) + } + """ + # and then transform it using jq + transforms: [ + { + pathpattern: ["address", "location"] + editor: """ + jq:{lat:.[0],lon:.[1]} + """ } - """ - # and then transform it using jq - transforms: [{pathpattern: ["address","location"], editor: """ - jq:{lat:.[0],lon:.[1]} - """ - }] + ] ) -} \ No newline at end of file +} diff --git a/transforms/jsonarrayToJsonobject/index.graphql b/transforms/jsonarrayToJsonobject/index.graphql index 458a6cd..956ed98 100644 --- a/transforms/jsonarrayToJsonobject/index.graphql +++ b/transforms/jsonarrayToJsonobject/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/transforms/jsonobjectToJsonarray/api.graphql b/transforms/jsonobjectToJsonarray/api.graphql index 0ea4c40..763a5bf 100644 --- a/transforms/jsonobjectToJsonarray/api.graphql +++ b/transforms/jsonobjectToJsonarray/api.graphql @@ -7,60 +7,64 @@ # In the second query, we further transform the object to set the right fields. type Customer { - id: ID - fullName: String - age: Int + id: ID + fullName: String + age: Int } type Query { - # This query converts a structure {key1:value1,key2:value2,...} - # to [{"name":key1, "value": value1},{"name":key2, "value": value2},...]. - customerAsNameValue: JSON - @rest ( - # The `ecmascript` argument is great for writing filters. - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. - # We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. - # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. - endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - return(JSON.stringify({"1":{"fullName":"John Doe", "age":23}, "2":{"fullName":"Jane Smith", age: 25}})) - } - """ - # Then we transform it using `objectToArray`. - # The `pathpattern` contains only one entry, which means it is applied at the root. - # The pathpattern "<>*" means that at this place, one would find a sequence of zero or more object keys. - transforms: [{pathpattern: ["<>*"], editor: "objectToArray"}] + # This query converts a structure {key1:value1,key2:value2,...} + # to [{"name":key1, "value": value1},{"name":key2, "value": value2},...]. + customerAsNameValue: JSON + @rest( + # The `ecmascript` argument is great for writing filters. + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on `ecmascript`. + # We are picturing a real-life scenario in which you call a backend using the `endpoint` argument. + # See https://stepzen.com/docs/connecting-backends/how-to-connect-a-rest-service for more information on `@rest`. + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + return(JSON.stringify({"1":{"fullName":"John Doe", "age":23}, "2":{"fullName":"Jane Smith", age: 25}})) + } + """ + # Then we transform it using `objectToArray`. + # The `pathpattern` contains only one entry, which means it is applied at the root. + # The pathpattern "<>*" means that at this place, one would find a sequence of zero or more object keys. + transforms: [{ pathpattern: ["<>*"], editor: "objectToArray" }] ) - # This query first converts a structure {key1:value1,key2:value2,...} - # to [{"name":key1, "value": value1},{"name":key2, "value": value2},...]. - # Then it applies a second transformation to set the right fields of the return type. - # In this case, the second transformation can also be done using the `setters` argument, - # as you can see in the commented-out portion. - # For more information on setters, see https://stepzen.com/docs/custom-graphql-directives/directives#setters. - customerAsCustomer: JSON - @rest ( - # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on using `ecmascript`. - endpoint: "stepzen:empty" - ecmascript: """ - function transformREST(s) { - return(JSON.stringify({"1":{"fullName":"John Doe", "age":23}, "2":{"fullName":"Jane Smith", age: 25}})) + # This query first converts a structure {key1:value1,key2:value2,...} + # to [{"name":key1, "value": value1},{"name":key2, "value": value2},...]. + # Then it applies a second transformation to set the right fields of the return type. + # In this case, the second transformation can also be done using the `setters` argument, + # as you can see in the commented-out portion. + # For more information on setters, see https://stepzen.com/docs/custom-graphql-directives/directives#setters. + customerAsCustomer: JSON + @rest( + # See https://stepzen.com/docs/custom-graphql-directives/directives#ecmascript for more information on using `ecmascript`. + endpoint: "stepzen:empty" + ecmascript: """ + function transformREST(s) { + return(JSON.stringify({"1":{"fullName":"John Doe", "age":23}, "2":{"fullName":"Jane Smith", age: 25}})) + } + """ + # Transform the JSON using objectToArray. + # Here the `pathpattern` contains only one entry, which means it is applied at the root. + # The pathpattern "<>*" means that at this place, one would find a sequence of zero or more object keys. + # Then, do another transformation to convert the name, and value pair it to the right structure. + # For the second transformation, it also contains only one entry, which means it is applied at the root. + # The pathpattern "[]*" means at this place, one would find a sequence of zero or more objects. + # The jq expression says to treat that zero or more objects as an array and transform each element of the array. + # 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.name,age:.value.age}" } - """ - # Transform the JSON using objectToArray. - # Here the `pathpattern` contains only one entry, which means it is applied at the root. - # The pathpattern "<>*" means that at this place, one would find a sequence of zero or more object keys. - # Then, do another transformation to convert the name, and value pair it to the right structure. - # For the second transformation, it also contains only one entry, which means it is applied at the root. - # The pathpattern "[]*" means at this place, one would find a sequence of zero or more objects. - # The jq expression says to treat that zero or more objects as an array and transform each element of the array. - # 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.name,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. - # setters: [{field: "id", path: "name"}, {field:"fullName", path: "value.age"}, {field:"age", path:"value.fullName"}] - ) -} \ No newline at end of file + ] + # for the second transformation, you can use setters. Uncomment the next line and comment the second pathpattern line above to see how it operates. + # setters: [{field: "id", path: "name"}, {field:"fullName", path: "value.age"}, {field:"age", path:"value.fullName"}] + ) +} diff --git a/transforms/jsonobjectToJsonarray/index.graphql b/transforms/jsonobjectToJsonarray/index.graphql index 458a6cd..956ed98 100644 --- a/transforms/jsonobjectToJsonarray/index.graphql +++ b/transforms/jsonobjectToJsonarray/index.graphql @@ -1,3 +1,3 @@ -schema @sdl(files:[ - "api.graphql" - ]){query: Query} \ No newline at end of file +schema @sdl(files: ["api.graphql"]) { + query: Query +}