Skip to content

Commit 35ed095

Browse files
authored
Merge pull request #65 from stepzen-dev/jayaram/value-directive
chore: @value snippets
2 parents 6b92d5c + a804d3c commit 35ed095

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ The snippets are generally broken up into functional areas, with each folder cov
6363
- [executable](executable) - How GraphQL _executable documents_ can be registered and used with a schema or endpoint.
6464
- @supplies
6565
- [routing](routing)
66+
- @value
67+
- [value](value)
6668

6769
### General topics
6870

value/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# @value
2+
3+
The `@value` directive defines a value that can be applied in various contexts, each with specific behaviors.
4+
- When applied to a field, selecting the field will return the specified value.
5+
6+
If no arguments are provided (`@value`) then the value is `null`.
7+
8+
View the [documentation](https://www.ibm.com/docs/en/api-connect/ace/saas?topic=directives-directive-value) on the custom directive `@value`.
9+
10+
## Snippets
11+
12+
- [constants](constants) shows how sets the element to a constant value

value/constants/api.graphql

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# A `@value` directive defines a value that can be applied in various contexts
2+
# To establish a constant value for a field, we can utilize this directive.
3+
# If no arguments are provided (@value) then the value is null.
4+
5+
type Customer {
6+
name: String!
7+
city: String!
8+
}
9+
10+
# To establish a constant value for the state, utilize `@value` and provide the constant value.
11+
# xid will always resolve to null.
12+
# joinDate returns a constant value using the @value annotation.
13+
# createdDate returns a constant value using the @value annotation.
14+
extend type Customer{
15+
state:String @value(const:"Florida")
16+
xid:ID @value
17+
joinDate: Date @value(const: "2024-02-24")
18+
createdDate: DateTime @value(const: "2024-02-24T07:20:50.52Z")
19+
}
20+
21+
22+
type Query {
23+
# set the default value
24+
customer(id: ID): Customer
25+
@value(
26+
script: {
27+
src: """
28+
Object({name:'John Doe',city:'Miami'})
29+
"""
30+
language: ECMASCRIPT
31+
}
32+
)
33+
34+
35+
# To concatenate strings using @value in jsonata
36+
concat(a: String, b: String): String
37+
@value(
38+
script: {
39+
src: """
40+
$join([a,b], "-")
41+
"""
42+
language: JSONATA
43+
}
44+
)
45+
}
46+
47+
# JSON scalars with @value
48+
extend type Query {
49+
json_string: JSON @value(const: "goodbye")
50+
json_list: JSON @value(const: [2, "hi"])
51+
}
52+
53+
# Constant scalar values
54+
type Query {
55+
returnBoolean: Boolean @value(const: true)
56+
integer: Int @value(const: 94)
57+
float: Float @value(const: 241.8)
58+
}
59+

value/constants/index.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
schema @sdl(files: ["api.graphql"]) {
2+
query: Query
3+
}

value/constants/stepzen.config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"endpoint": "api/miscellaneous"
3+
}

value/constants/tests/Test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const {
2+
deployAndRun,
3+
stepzen,
4+
getTestDescription,
5+
} = require("../../../tests/gqltest.js");
6+
7+
testDescription = getTestDescription("snippets", __dirname);
8+
9+
describe(testDescription, function () {
10+
const tests = [
11+
{ label: "customer(1) return default name and city ",
12+
query: '{customer(id:1){name city }}',
13+
expected: {customer: {name:'John Doe',city:'Miami'}},
14+
},
15+
{ label: "customer(2) with pass default state value",
16+
query: '{customer(id:2){name city state }}',
17+
expected: {customer: {name:'John Doe',city:'Miami',state:"Florida"}},
18+
},
19+
{ label: "customer(3) xid returns null value",
20+
query: '{customer(id:2){name city state xid }}',
21+
expected: {customer: {name:'John Doe',city:'Miami',state:"Florida",xid:null}},
22+
},
23+
{ label: "customer(4) joinDate returns const value",
24+
query: '{customer(id:2){name city joinDate }}',
25+
expected: {customer: {name:'John Doe',city:'Miami',joinDate:'2024-02-24'}},
26+
},
27+
{ label: "customer(5) createdDate returns const value",
28+
query: '{customer(id:2){name city createdDate }}',
29+
expected: {customer: {name:'John Doe',city:'Miami',createdDate:'2024-02-24T07:20:50.52Z'}},
30+
},
31+
{ label: "concat string",
32+
query: '{concat(a: "Steve",b:"Jobs" )}',
33+
expected: {concat: 'Steve-Jobs'},
34+
},
35+
{ label: "JSON scalars return json_string",
36+
query: '{json_string}',
37+
expected: {"json_string": "goodbye"},
38+
},
39+
{ label: "JSON scalars return json_list",
40+
query: '{json_list}',
41+
expected: {"json_list": [2,"hi"]},
42+
},
43+
{ label: "return const true boolean value ",
44+
query: '{returnBoolean}',
45+
expected: {"returnBoolean": true},
46+
},
47+
{ label: "return const integer value ",
48+
query: '{integer}',
49+
expected: {"integer": 94},
50+
},
51+
{ label: "return const float value ",
52+
query: '{float}',
53+
expected: {"float": 241.8},
54+
},
55+
]
56+
return deployAndRun(__dirname, tests, stepzen.admin);
57+
});
58+

0 commit comments

Comments
 (0)