question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow applyMiddlewareToField to spread extra properties or add specific key for extra metadata

See original GitHub issue

Currently applyMiddlewareToField only spreads the properties it wants to:

https://github.com/prisma/graphql-middleware/blob/500925ade884a025cb6eb2ba99672c4f4a52b13c/src/applicator.ts#L41-L84

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

https://github.com/apollographql/graphql-tools/blob/3b77c2071e4b2328b1a4c4471c61d4ad93a6ae57/src/generate/addResolveFunctionsToSchema.ts#L140-L147

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

Do not report excess property errors on spread #19775
I'm using this to get excess property checking to work when assigning an object of one type to another. const a: TypeA; const...
Read more >
Is there an option to make spreading an object strict?
Interesting question. According to this issue, the result of the spread operator is intended to not trigger excess property checks.
Read more >
Configuration Metadata
Spring Boot jars include metadata files that provide details of all supported configuration properties. The files are designed to let IDE ...
Read more >
JS: use spread to exclude properties
The spread operator in JavaScript can be very useful. For example to create a copy of an object: const firstObject = {id: 0,...
Read more >
An Easy Guide to Object Rest/Spread Properties in ...
Object spread allows easily to clone, merge, extend objects. ... An additional spread ...laptop.screen ensures that the nested object is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found