Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolvers-composition: do not mutate resolvers #4722

Open
Tracked by #5201 ...
ghost opened this issue Sep 20, 2022 · 3 comments
Open
Tracked by #5201 ...

resolvers-composition: do not mutate resolvers #4722

ghost opened this issue Sep 20, 2022 · 3 comments

Comments

@ghost
Copy link

ghost commented Sep 20, 2022

Currently, composeResolvers mutates the resolvers argument. I am requesting that this behaviour be changed in a future major version, or add a setting so it can be set to not mutate.

@ardatan
Copy link
Owner

ardatan commented Sep 20, 2022

I am not sure I understand your issue because we cannot compose resolvers without mutating them. If you have any idea about doing that differently, feel free to create a PR.

@ghost
Copy link
Author

ghost commented Sep 20, 2022

I am trying to use the composeResolvers function as a replacement for graphql-middleware's applyMiddleware function.

Modifying the example from that package's README, I would have expected something like this to work:

const typeDefs = `
type Query {
  hello(name: String): String
  bye(name: String): String
}
`
const resolvers = {
  Query: {
    hello: (root, args, context, info) => {
      console.log(`3. resolver: hello`)
      return `Hello ${args.name ? args.name : 'world'}!`
    },
    bye: (root, args, context, info) => {
      console.log(`3. resolver: bye`)
      return `Bye ${args.name ? args.name : 'world'}!`
    },
  },
}

const logInput = (next) => async (root, args, context, info) => {
  console.log(`1. logInput: ${JSON.stringify(args)}`)
  const result = await next(root, args, context, info)
  console.log(`5. logInput`)
  return result
}

const logResult = (next) => async (root, args, context, info) => {
  console.log(`2. logResult`)
  const result = await next(root, args, context, info)
  console.log(`4. logResult: ${JSON.stringify(result)}`)
  return result
}

const resolversWithMiddleware = composeResolvers(resolvers, {
  '*.*': [logInput, logResult],
});

const serverWithMiddleware = new ApolloServer({
  schema: makeExecutableSchema({ typeDefs, resolvers: resolversWithMiddleware }),
})

const serverWithoutMiddleware = new ApolloServer({
  schema: makeExecutableSchema({ typeDefs, resolvers })
})

But the serverWithoutMiddleware server has the middleware applied, because composeResolvers mutated resolvers.

@ardatan
Copy link
Owner

ardatan commented Sep 20, 2022

Feel free to create a PR to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant