Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: @value snippets #65

Merged
merged 11 commits into from
Nov 4, 2024
Merged
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
12 changes: 12 additions & 0 deletions value/README.md
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions value/constants/api.graphql
Original file line number Diff line number Diff line change
@@ -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)
}

3 changes: 3 additions & 0 deletions value/constants/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/constants/stepzen.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"endpoint": "api/miscellaneous"
}
58 changes: 58 additions & 0 deletions value/constants/tests/Test.js
Original file line number Diff line number Diff line change
@@ -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);
});