如何解决有没有办法在另一个 GraphQL 查询解析器中调用 GraphQL 查询?
我是 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 举报,一经查实,本站将立刻删除。