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