diff --git a/README.md b/README.md index 4b480ad..62da0e5 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,8 @@ The snippets are generally broken up into functional areas, with each folder cov - [executable](executable) - How GraphQL _executable documents_ can be registered and used with a schema or endpoint. - @supplies - [routing](routing) +- @value + - [value](value) ### General topics diff --git a/value/README.md b/value/README.md new file mode 100644 index 0000000..877f5fa --- /dev/null +++ b/value/README.md @@ -0,0 +1,12 @@ +# @value + +The `@value` directive defines a value that can be applied in various contexts, each with specific behaviors. + - When applied to a field, selecting the field will return the specified value. + +If no arguments are provided (`@value`) then the value is `null`. + +View the [documentation](https://www.ibm.com/docs/en/api-connect/ace/saas?topic=directives-directive-value) on the custom directive `@value`. + +## Snippets + +- [constants](constants) shows how sets the element to a constant value diff --git a/value/constants/api.graphql b/value/constants/api.graphql new file mode 100644 index 0000000..bf9b039 --- /dev/null +++ b/value/constants/api.graphql @@ -0,0 +1,59 @@ +# A `@value` directive defines a value that can be applied in various contexts +# To establish a constant value for a field, we can utilize this directive. +# If no arguments are provided (@value) then the value is null. + +type Customer { + name: String! + city: String! +} + +# To establish a constant value for the state, utilize `@value` and provide the constant value. +# xid will always resolve to null. +# joinDate returns a constant value using the @value annotation. +# createdDate returns a constant value using the @value annotation. +extend type Customer{ + state:String @value(const:"Florida") + xid:ID @value + joinDate: Date @value(const: "2024-02-24") + createdDate: DateTime @value(const: "2024-02-24T07:20:50.52Z") +} + + +type Query { + # set the default value + customer(id: ID): Customer + @value( + script: { + src: """ + Object({name:'John Doe',city:'Miami'}) + """ + language: ECMASCRIPT + } + ) + + + # To concatenate strings using @value in jsonata + concat(a: String, b: String): String + @value( + script: { + src: """ + $join([a,b], "-") + """ + language: JSONATA + } + ) +} + +# JSON scalars with @value +extend type Query { + json_string: JSON @value(const: "goodbye") + json_list: JSON @value(const: [2, "hi"]) +} + +# Constant scalar values +type Query { + returnBoolean: Boolean @value(const: true) + integer: Int @value(const: 94) + float: Float @value(const: 241.8) +} + diff --git a/value/constants/index.graphql b/value/constants/index.graphql new file mode 100644 index 0000000..956ed98 --- /dev/null +++ b/value/constants/index.graphql @@ -0,0 +1,3 @@ +schema @sdl(files: ["api.graphql"]) { + query: Query +} diff --git a/value/constants/stepzen.config.json b/value/constants/stepzen.config.json new file mode 100644 index 0000000..bfa1d76 --- /dev/null +++ b/value/constants/stepzen.config.json @@ -0,0 +1,3 @@ +{ + "endpoint": "api/miscellaneous" +} diff --git a/value/constants/tests/Test.js b/value/constants/tests/Test.js new file mode 100644 index 0000000..6a9a878 --- /dev/null +++ b/value/constants/tests/Test.js @@ -0,0 +1,58 @@ +const { + deployAndRun, + stepzen, + getTestDescription, + } = require("../../../tests/gqltest.js"); + + testDescription = getTestDescription("snippets", __dirname); + + describe(testDescription, function () { + const tests = [ + { label: "customer(1) return default name and city ", + query: '{customer(id:1){name city }}', + expected: {customer: {name:'John Doe',city:'Miami'}}, + }, + { label: "customer(2) with pass default state value", + query: '{customer(id:2){name city state }}', + expected: {customer: {name:'John Doe',city:'Miami',state:"Florida"}}, + }, + { label: "customer(3) xid returns null value", + query: '{customer(id:2){name city state xid }}', + expected: {customer: {name:'John Doe',city:'Miami',state:"Florida",xid:null}}, + }, + { label: "customer(4) joinDate returns const value", + query: '{customer(id:2){name city joinDate }}', + expected: {customer: {name:'John Doe',city:'Miami',joinDate:'2024-02-24'}}, + }, + { label: "customer(5) createdDate returns const value", + query: '{customer(id:2){name city createdDate }}', + expected: {customer: {name:'John Doe',city:'Miami',createdDate:'2024-02-24T07:20:50.52Z'}}, + }, + { label: "concat string", + query: '{concat(a: "Steve",b:"Jobs" )}', + expected: {concat: 'Steve-Jobs'}, + }, + { label: "JSON scalars return json_string", + query: '{json_string}', + expected: {"json_string": "goodbye"}, + }, + { label: "JSON scalars return json_list", + query: '{json_list}', + expected: {"json_list": [2,"hi"]}, + }, + { label: "return const true boolean value ", + query: '{returnBoolean}', + expected: {"returnBoolean": true}, + }, + { label: "return const integer value ", + query: '{integer}', + expected: {"integer": 94}, + }, + { label: "return const float value ", + query: '{float}', + expected: {"float": 241.8}, + }, + ] + return deployAndRun(__dirname, tests, stepzen.admin); + }); +