Ad

How To Write A Private Function In Resolver(query Or Mutation)?

- 1 answer

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.

Ad

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.

Ad
source: stackoverflow.com
Ad