Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new to GraphQL, so i don't know all its basics so here is my question:
Is it possible to call one GraphQL query in another GraphQL query resolver? I have to mention that i use GraphQL with sequelize.
To be more specific, i have simple bank system schema where a user has many accounts and each account has transactions. I have a simple query defined in 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
                    }
                }
    }

Now, i have a mutation that needs to use the upper query (defined in 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
                        }
                    },
                    resolve: async (_, { 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();
                        }
             }
        }

In 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,
}));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
413 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...