Skip to content

Commit adfb8dc

Browse files
committed
@value snippets added
1 parent 0808d91 commit adfb8dc

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed

value/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
- [arguments](arguments) shows how sets the element to a constant value
13+
- [useOfJSON](useOfJSON) shows how to use JSONata language inside `@value` directive

value/arguments/api.graphql

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# A `@value` directive defines a value that can be applied in various contexts
2+
# To establish a constant value within a type or query, we can utilize this directive.
3+
# If no arguments are provided (@value) then the value is null.
4+
5+
enum Direction {
6+
NORTH
7+
EAST
8+
SOUTH
9+
WEST
10+
}
11+
12+
type Customer {
13+
name: String!
14+
city: String!
15+
}
16+
17+
# To establish a constant value for the state, utilize `@value` and provide the constant value.
18+
extend type Customer{
19+
state:String @value(const:"Florida")
20+
}
21+
22+
23+
type Query {
24+
emptyCustomer(id: ID): Customer @value # return null value
25+
customer(id: ID): Customer # set the default value
26+
@value(
27+
script: {
28+
src: """
29+
Object({name:'John Doe',city:'Miami'})
30+
"""
31+
language: ECMASCRIPT
32+
}
33+
)
34+
# To pass a constant value directly in a query and return the result
35+
pi:Float! @value(const:3.14159)
36+
37+
# return single emu value
38+
direction: Direction @value(script: {src: "\"EAST\""})
39+
40+
# @override with @value
41+
override_value(id: ID): Customer
42+
@override(from: "elsewhere")
43+
@value(script: {src: "Object({name: 'mark'})"})
44+
}

value/arguments/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+
}
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/arguments/tests/Test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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: "emptyCustomer with return null",
12+
query: '{emptyCustomer(id:1){name city }}',
13+
expected: {emptyCustomer: null},
14+
},
15+
{ label: "customer(1)",
16+
query: '{customer(id:1){name city }}',
17+
expected: {customer: {name:'John Doe',city:'Miami'}},
18+
},
19+
{ label: "customer(2) with pass default state value",
20+
query: '{customer(id:2){name city state }}',
21+
expected: {customer: {name:'John Doe',city:'Miami',state:"Florida"}},
22+
},
23+
{ label: "return pi const value 3.14159",
24+
query: '{pi}',
25+
expected: {pi:3.14159},
26+
},
27+
{ label: "return direction static value",
28+
query: '{direction}',
29+
expected: {direction:'EAST'},
30+
},
31+
{ label: "return customer(3) with @override ",
32+
query: '{override_value(id:3){name}}',
33+
expected: {override_value:{name:"mark"}},
34+
}
35+
]
36+
return deployAndRun(__dirname, tests, stepzen.admin);
37+
});
38+

value/useOfJSON/api.graphql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Here are examples demonstrating how to use the `@value` directive for returning JSON values.
2+
3+
4+
type Query {
5+
# To sum a and b using the @value directive within src in JSONata,
6+
sum(a: Int!, b: Int!): Int! @value(script: {src: "a+b", language: JSONATA})
7+
# To convert a string to lowercase format, you can utilize the lowercase() function in JSONata using @value
8+
lower(value: String): String
9+
@value(script: {src: "$lowercase(value)", language: JSONATA})
10+
# To convert a string to lowercase format, you can utilize the lowercase() function in JSONata using @value
11+
upper(value: String): String
12+
@value(script: {src: "$uppercase(value)", language: JSONATA})
13+
14+
# To concatenate strings using @value in JSONata
15+
concat(a: String, b: String): String
16+
@value(
17+
script: {
18+
src: """
19+
$join([a,b], "-")
20+
"""
21+
language: JSONATA
22+
}
23+
)
24+
}

value/useOfJSON/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+
}
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/useOfJSON/tests/Test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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: "return sum of two input using @value with JSONATA",
12+
query: '{sum(a: 10, b: 10)}',
13+
expected: {sum: 20},
14+
},
15+
{ label: "convert lower string",
16+
query: '{lower(value: "MIAMI")}',
17+
expected: {lower: 'miami'},
18+
},
19+
{ label: "convert upper string",
20+
query: '{upper(value: "miami")}',
21+
expected: {upper: 'MIAMI'},
22+
},
23+
{ label: "concat string",
24+
query: '{concat(a: "Steve",b:"Jobs" )}',
25+
expected: {concat: 'Steve-Jobs'},
26+
},
27+
]
28+
return deployAndRun(__dirname, tests, stepzen.admin);
29+
});
30+

0 commit comments

Comments
 (0)