Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions value/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @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

- [arguments](arguments) shows how sets the element to a constant value
- [useOfJSON](useOfJSON) shows how to use JSONata language inside `@value` directive
44 changes: 44 additions & 0 deletions value/arguments/api.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# A `@value` directive defines a value that can be applied in various contexts
# To establish a constant value within a type or query, we can utilize this directive.
# If no arguments are provided (@value) then the value is null.

enum Direction {
NORTH
EAST
SOUTH
WEST
}

type Customer {
name: String!
city: String!
}

# To establish a constant value for the state, utilize `@value` and provide the constant value.
extend type Customer{
state:String @value(const:"Florida")
}


type Query {
emptyCustomer(id: ID): Customer @value # return null value
customer(id: ID): Customer # set the default value
@value(
script: {
src: """
Object({name:'John Doe',city:'Miami'})
"""
language: ECMASCRIPT
}
)
# To pass a constant value directly in a query and return the result
pi:Float! @value(const:3.14159)

# return single emu value
direction: Direction @value(script: {src: "\"EAST\""})

# @override with @value
override_value(id: ID): Customer
@override(from: "elsewhere")
@value(script: {src: "Object({name: 'mark'})"})
}
3 changes: 3 additions & 0 deletions value/arguments/index.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema @sdl(files: ["api.graphql"]) {
query: Query
}
3 changes: 3 additions & 0 deletions value/arguments/stepzen.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"endpoint": "api/miscellaneous"
}
38 changes: 38 additions & 0 deletions value/arguments/tests/Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const {
deployAndRun,
stepzen,
getTestDescription,
} = require("../../../tests/gqltest.js");

testDescription = getTestDescription("snippets", __dirname);

describe(testDescription, function () {
const tests = [
{ label: "emptyCustomer with return null",
query: '{emptyCustomer(id:1){name city }}',
expected: {emptyCustomer: null},
},
{ label: "customer(1)",
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: "return pi const value 3.14159",
query: '{pi}',
expected: {pi:3.14159},
},
{ label: "return direction static value",
query: '{direction}',
expected: {direction:'EAST'},
},
{ label: "return customer(3) with @override ",
query: '{override_value(id:3){name}}',
expected: {override_value:{name:"mark"}},
}
]
return deployAndRun(__dirname, tests, stepzen.admin);
});

24 changes: 24 additions & 0 deletions value/useOfJSON/api.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Here are examples demonstrating how to use the `@value` directive for returning JSON values.


type Query {
# To sum a and b using the @value directive within src in JSONata,
sum(a: Int!, b: Int!): Int! @value(script: {src: "a+b", language: JSONATA})
# To convert a string to lowercase format, use @value in JSONata
lower(value: String): String
@value(script: {src: "$lowercase(value)", language: JSONATA})
# To convert a string to uppercase format, use @value in JSONata
upper(value: String): String
@value(script: {src: "$uppercase(value)", language: JSONATA})

# To concatenate strings using @value in JSONata
concat(a: String, b: String): String
@value(
script: {
src: """
$join([a,b], "-")
"""
language: JSONATA
}
)
}
3 changes: 3 additions & 0 deletions value/useOfJSON/index.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema @sdl(files: ["api.graphql"]) {
query: Query
}
3 changes: 3 additions & 0 deletions value/useOfJSON/stepzen.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"endpoint": "api/miscellaneous"
}
30 changes: 30 additions & 0 deletions value/useOfJSON/tests/Test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const {
deployAndRun,
stepzen,
getTestDescription,
} = require("../../../tests/gqltest.js");

testDescription = getTestDescription("snippets", __dirname);

describe(testDescription, function () {
const tests = [
{ label: "return sum of two input using @value with JSONATA",
query: '{sum(a: 10, b: 10)}',
expected: {sum: 20},
},
{ label: "convert lower string",
query: '{lower(value: "MIAMI")}',
expected: {lower: 'miami'},
},
{ label: "convert upper string",
query: '{upper(value: "miami")}',
expected: {upper: 'MIAMI'},
},
{ label: "concat string",
query: '{concat(a: "Steve",b:"Jobs" )}',
expected: {concat: 'Steve-Jobs'},
},
]
return deployAndRun(__dirname, tests, stepzen.admin);
});