问题描述
我是 GraphQL 的新手,所以我不知道它的所有基础知识,所以这是我的问题:
是否可以在另一个 GraphQL 查询解析器中调用一个 GraphQL 查询?我不得不提一下,我将 GraphQL 与 sequelize 一起使用。
更具体地说,我有一个简单的银行系统架构,其中一个用户有多个账户,每个账户都有交易。我在 query.js 中定义了一个简单的查询
// query.js
const queryType = new GraphQLObjectType({
name: 'Query',fields: {
getMaxTransaction: {
type: GraphQLInt,// returns int
args: { // takes an argument the user id
userId: {
type: GraphQLNonNull(GraphQLInt)
}
},resolve: async (_,{ userId }) => {
// code
return 5; // just for the sake of the example
}
}
}
现在,我有一个需要使用上层查询的突变(在mutation.js中定义):
// mutation.js
const mutationType = new GraphQLObjectType({
name: 'Mutation',fields: {
createTransaction: {
type: GraphQLBoolean,args: {
iban_from: {
type: GraphQLString
},iban_to: {
type: GraphQLString
},sum: {
type: GraphQLFloat
}
},{ iban_from,iban_to,sum },context) => {
// problem is here. How do i call the query getMaxTransaction defined
earlier in another query / mutation resolver?
// i want to do something like const maxTransaction = getMaxTransaction();
}
}
}
在 index.js 中:
const { graphqlHTTP } = require('express-graphql');
const schema = require('./graphql'); // here are mutation.js and query.js defined
const authenticationMiddleware = require('./middlewares/authenticationMiddleware'); // simple middleware
app.use('/graphql',authenticationMiddleware,graphqlHTTP({
schema,}));
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)