Allow applyMiddlewareToField to spread extra properties or add specific key for extra metadata
See original GitHub issueCurrently applyMiddlewareToField
only spreads the properties it wants to:
It would be really useful if extra properties were also added to the final field definition, that way we could declare some custom properties that could be used directly in the middlewares.
For instance, we could have a middleware added to all mutations, but that only does something to mutations that have a specific flag.
// ...
const resolvers = {
// ...
Mutation: {
// ...
DoSomething: {
flags: [FLAGS.SPECIFIC_FLAG],
resolve: async (root, args, context, info)=> {
// ...
},
}
},
}
then on the middleware:
const customValidation = {
async Mutation(resolve, root, args, context, info) {
// use info to get the _typeConfig of the mutation being executed
const mutationConfig = ...;
if (mutationConfig.flags && mutationConfig.flags.includes(FLAGS.SPECIFIC_FLAG)) {
// Do something
}
}
}
I’m asking for that because I’m migrating some code that uses graphql-add-middleware to use graphql-middleware
instead.
I’m going to write a post about validations with graphql-middleware
, but for it to work I need this “feature”.
Thoughts?
I can get some time to work on that if it’s acceptable.
addResolveFunctionsToSchema
from graphql-tools
does spreads all other properties, so it’s not a problem.
See related code there: https://github.com/apollographql/graphql-tools/blob/3b77c2071e4b2328b1a4c4471c61d4ad93a6ae57/src/generate/addResolveFunctionsToSchema.ts#L121
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top GitHub Comments
@maticzav btw, I just published the post I mentioned above: https://medium.com/@jonathancardoso/graphql-mutation-arguments-validation-with-yup-using-graphql-middleware-645822fb748
@marktani I’ve just opened a PR for this: https://github.com/prisma/graphql-middleware/pull/26