graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

How to add middleware without directive?

Open featherbits opened this issue 8 years ago • 5 comments

I would like to hook custom delegate before field is resolved without decorating schema field with directive? Is that possible? Something like described in here https://hotchocolate.io/docs/next/directive-middleware#middleware but without actual directive. Something that I can add to schema configuration and it would execute before any field is resolved with possibility to override result of that field.

featherbits avatar Nov 26 '18 20:11 featherbits

Hi,

this would be actually this PR #363

Will come with the next version....

we are introducing then three kinds of middlewares:

  • Query Middleware This will basically allow you to rewrite and modify requests (this is needed for our schema stitching framework)

  • Field Middleware This will integrate to all field resolvers.

  • Directive Middleware This is a selective middleware that only includes itself if annotated

michaelstaib avatar Nov 26 '18 21:11 michaelstaib

we will provide a preview of this feature tomorrow night with 0.7.0-preview.2

michaelstaib avatar Nov 26 '18 21:11 michaelstaib

0.7.0-preview.2 is building now

Here is how to use it:

Schema.Create(c => {
   c.Use(next => context => 
   {
        next(context);
        if(context.Result is string s) 
        {
             context.Result = s.ToUpper();
        } 
   });
}); 

Field Middlewares are built into every field resolver during schema creation, so they will always run. This is opposed to directive middlewares.

So, an instrumentation middleware would be best implemented as directive middleware since it could be annotated to the whole request or just to problem fields to get performance insights.

Some other middlewares are better written as field middleware since those are always executed. resolver

michaelstaib avatar Nov 26 '18 21:11 michaelstaib

Please remember that we are still working on this feature. Query middlewares are not yet included and last but not least feedback is welcome :)

michaelstaib avatar Nov 26 '18 21:11 michaelstaib

I will close this issue... Feedback can be added to #338

michaelstaib avatar Nov 26 '18 21:11 michaelstaib