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

bind middleware to return types #551

Open
milad2golnia opened this issue Aug 21, 2022 · 0 comments
Open

bind middleware to return types #551

milad2golnia opened this issue Aug 21, 2022 · 0 comments

Comments

@milad2golnia
Copy link

milad2golnia commented Aug 21, 2022

Problem

Is it possible to assign my middleware to a specific return type? Let's elaborate it using an example.

Example

I'm defining my schema in SDL (Schema Definition Language):

let schema = buildschema(`
    schema {
        query: RootQuery,
        mutation: RootMutation
    }
    type service{...}
    type notservice{...}
    type RootQuery {
        query1:    service
        query2:    service
        query3:    notservice
    }
    type RootMutation{...}
`);

I want to execute my middleware when a service is returned, How can I do it?

Current Solution

The current way is to apply middleware one by one to every query which is returning service as result:

    let newSchema = applyMiddleware(querySchema, {
         RootQuery: {
             query1:     myMiddleware,
             query2:     myMiddleware
         }
    })

But this solution is not maintainable because if I later add another query(e.g. query4) with the same return type (i.e. service), I should again modify my code:

    let newSchema = applyMiddleware(querySchema, {
         RootQuery: {
             query1:     myMiddleware,
             query2:     myMiddleware,
             query4:     myMiddleware
         }
    })
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