How To Write A Private Function In Resolver(query Or Mutation)?
I use apollo server and graphql to develop back-end. I don't know how to write a private function in resolver, "private" means that it can't be called by front-end and only can be called by other functions in resolver.Ideally, I hope this function can be in Query or Mutation.
Mutation {
addFlowHistroy(id: ID!): FlowHistory
changeFlowStatus(ied: ID!) FlowLog
}
I want addFlowHistroy
can be called by changeFlowStatus
, and not to be exposed to front-end. What should I do? Thanks.
Answer
There is no mechanism for calling a resolver inside another one and there should never be a need to do so. If you have some logic that is shared between resolvers, you can extract it into its own function and just call that function directly inside each resolver -- there's no need to add a field to your schema if it is not going to be consumed by a client.
const typeDefs = gql`
type Query {
four: Int!
nine: Int!
}
`
const resolvers = {
Query: {
foo: () => square(2),
bar: () => square(3),
},
}
function square(x) {
return x * x
}
If the shared logic represents business logic, it should probably exist inside a domain model or service. Your resolvers would then just call the appropriate method on that model or service.
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error