GraphQL-返回取决于参数的计算类型 [英] GraphQL - return calculated type dependent on argument
问题描述
概述(简体):
在NodeJS服务器中,我实现了以下GraphQL模式:
In my NodeJS server I've implemented the following GraphQL schema:
type Item {
name: String,
value: Float
}
type Query {
items(names: [String]!): [Item]
}
然后,客户端查询传递一个名称数组作为参数:
The client query then passes an array of names, as an argument:
{
items(names: ["total","active"] ) {
name
value
}
}
后端API向mysql数据库查询"总计"和"活动"字段(我的数据库表中的列),并减少响应,如下所示:
The backend API queries a mysql DB, for the "total" and "active" fields (columns on my DB table) and reduces the response like so:
[{"name":"total" , value:100} , {"name":"active" , value:50}]
我希望我的graphQL API支持比率"项,即:我想发送以下查询:
I would like my graphQL API to support "ratio" Item, I.E: I would like to send the following query:
{
items(names: ["ratio"] ) {
name
value
}
}
或
{
items(names: ["total","active","ratio"] ) {
name
value
}
}
并返回 active/total 作为该新字段([{"name":"ratio" , value:0.5}]
)的计算结果.以不同的方式处理"比率"字段的通用方法是什么?
And return active / total as the calculated result of that new field ([{"name":"ratio" , value:0.5}]
). What would be a generic way to handle the "ratio" field differently?
应该是我的架构中的新类型,还是应该在化简器中实现逻辑?
Should it be a new type in my schema or should I implement the logic in the reducer?
推荐答案
Joe的答案(一旦从数据库中获取结果后,将{"name":"ratio" , value:data.active/data.total}
追加到结果中)将无需进行任何模式更改.
Joe's answer (append {"name":"ratio" , value:data.active/data.total}
to the result once the result is fetched from database) would do it without making any schema changes.
作为替代方法或在GraphQL中更优雅的方法,可以在类型本身中指定字段名称,而不用将其作为参数传递.然后通过编写解析器来计算ratio
.
As an alternative method or as a more elegant way to do it in GraphQL, the field names can be specified in the type itself instead of passing them as arguments. And compute ratio
by writing a resolver.
因此,GraphQL模式将是:
So, the GraphQL schema would be:
Item {
total: Int,
active: Int,
ratio: Float
}
type Query {
items: [Item]
}
客户端指定字段:
{
items {
total
active
ratio
}
}
ratio
可以在解析器内部计算.
And ratio
can be calculated inside the resolver.
这是代码:
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { graphql } = require('graphql');
const { makeExecutableSchema } = require('graphql-tools');
const getFieldNames = require('graphql-list-fields');
const typeDefs = `
type Item {
total: Int,
active: Int,
ratio: Float
}
type Query {
items: [Item]
}
`;
const resolvers = {
Query: {
items(obj, args, context, info) {
const fields = getFieldNames(info) // get the array of field names specified by the client
return context.db.getItems(fields)
}
},
Item: {
ratio: (obj) => obj.active / obj.total // resolver for finding ratio
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const db = {
getItems: (fields) => // table.select(fields)
[{total: 10, active: 5},{total: 5, active: 5},{total: 15, active: 5}] // dummy data
}
graphql(
schema,
`query{
items{
total,
active,
ratio
}
}`,
{}, // rootValue
{ db } // context
).then(data => console.log(JSON.stringify(data)))
这篇关于GraphQL-返回取决于参数的计算类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!