小编am0*_*100的帖子

在 GraphQL 查询和模式中表示计算/脚本函数

我们使用 GraphQL 作为数据聚合引擎的​​查询语言。

我正在寻找在 GraphQL 中表示简单(或复杂)算术计算函数的想法,参考模式中定义的现有类型/属性,并且可以在现有属性上使用。

我正在研究自定义标量和指令

例子 -

{
    item{
        units
        price_per_unit
        market_price: function:multiply(units, price_per_unit)
        market_price_usd: function:usdPrice(units, price_per_unit, currency)
    }
}
Run Code Online (Sandbox Code Playgroud)

其中 function:multiply 已在GraphQL架构中定义为类型

functions {
    multiply(operand 1, operand2) {
        result
    }
    usdPrice(operand1, operand2, currency) {
        result: {
                if(currency == GBP) {
                    operand1 * operand2 * .76
                }
            {
    }
Run Code Online (Sandbox Code Playgroud)

内部解析器将操作数 1 和操作数 2 相乘以创建结果。

graphql graphql-java

5
推荐指数
1
解决办法
1705
查看次数

标签 统计

graphql ×1

graphql-java ×1