graphql-tools
graphql-tools copied to clipboard
Multiple directives not working in the sequence as defined in the graphql query
Issue workflow progress
Progress of the issue based on the Contributor Workflow
- [ ] 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox
Make sure to fork this template and run
yarn generatein the terminal.Please make sure the GraphQL Tools package versions under
package.jsonmatches yours. - [ ] 2. A failing test has been provided
- [ ] 3. A local solution has been provided
- [ ] 4. A pull request is pending review
Describe the bug
Multiple directives has been defined in apollo v4
const directivesObj = { isAuthenticated: authDirectiveTransformer, hasRole: hasRoleDirectiveTransformer, rateLimit: rateLimitDirectiveTransformer, };
const applyDirective = schema => { for (const directive in directivesObj) { schema = directivesObj[directive](schema, directive); } return schema; };
let schema = makeExecutableSchema({ typeDefs, resolvers, });
schema = applyDirective(schema);
the directives are working in the sequence as mentioned in the directivesObj from bottom to top, not as they are defined in the graphql query. I have checked the apolloV4 documentation, but there is only a single directive example.
Need to know how the apolloV4 handles multiple directives to apply to single query.
Expected behavior
It should be worked dynamically applied to the graphql query not as defined in the schema object.
Environment:
@graphql-tools/...:- "@graphql-tools/load-files": "6.6.1", "@graphql-tools/merge": "8.4.2", "@graphql-tools/schema": "10.0.0", "@graphql-tools/utils": "10.0.0",
- NodeJS: version 20.2.0
Could you share a reproduction on CodeSandbox? Thanks!
@ardatan please look into the below reproduction on CodeSandbox https://codesandbox.io/s/apollo-server-v4-d5nwml
For me it works with @graphql-tools/schema 7.1.5 but not 8.0.0 in combination with @graphql-tools/utils on 7.10.0
Working
nigelss@Nigels-MacBook-Pro XXX-api % npm ls @graphql-tools/utils
[email protected] /Users/nigelss/Code/XXX-api
├─┬ @graphql-tools/[email protected]
│ └── @graphql-tools/[email protected] deduped
├── @graphql-tools/[email protected]
└─┬ [email protected]
└─┬ [email protected]
├─┬ @graphql-tools/[email protected]
│ ├─┬ @graphql-tools/[email protected]
│ │ ├─┬ @graphql-tools/[email protected]
│ │ │ └── @graphql-tools/[email protected] deduped
│ │ └── @graphql-tools/[email protected] deduped
│ └── @graphql-tools/[email protected]
└─┬ @graphql-tools/[email protected]
├─┬ @graphql-tools/[email protected]
│ └── @graphql-tools/[email protected] deduped
└── @graphql-tools/[email protected]
Not working
nigelss@Nigels-MacBook-Pro XXX-api % npm ls @graphql-tools/utils
[email protected] /Users/nigelss/Code/XXX-api
├─┬ @graphql-tools/[email protected]
│ ├─┬ @graphql-tools/[email protected]
│ │ └── @graphql-tools/[email protected]
│ └── @graphql-tools/[email protected]
├── @graphql-tools/[email protected]
└─┬ [email protected]
└─┬ [email protected]
├─┬ @graphql-tools/[email protected]
│ ├─┬ @graphql-tools/[email protected]
│ │ ├─┬ @graphql-tools/[email protected]
│ │ │ └── @graphql-tools/[email protected] deduped
│ │ └── @graphql-tools/[email protected] deduped
│ └── @graphql-tools/[email protected]
└─┬ @graphql-tools/[email protected]
├─┬ @graphql-tools/[email protected]
│ └── @graphql-tools/[email protected] deduped
└── @graphql-tools/[email protected]
For me it could be due to graphql-tools changes from v7 to v8. Possibly need to be using transformer functions instead?
- https://community.apollographql.com/t/what-is-up-with-schema-directives-right-now/1031/3
- https://github.com/apollographql/apollo-server/issues/5607
- https://github.com/apollographql/apollo-server/pull/5621
also seeing this error, just migrated to the schema directives as per the current documentation https://the-guild.dev/graphql/tools/docs/schema-directives .
eg
myQuery @isAuthorised(role='participant') @authenticated
@authenticated validates the jwt and gets the user @isAuthorised the works out if they have permission to do what they are requesting.
isAuthorised is loaded first (obviously we can change the order) but this is just one example, we have many chained directives that need to run in order. Any assistance greatly appreciated
versions ├─┬ @graphql-tools/[email protected] │ ├─┬ @graphql-tools/[email protected] │ │ └── @graphql-tools/[email protected] deduped │ └── @graphql-tools/[email protected] deduped ├── @graphql-tools/[email protected] ├─┬ [email protected] │ └─┬ @graphql-tools/[email protected] │ ├─┬ @graphql-tools/[email protected] │ │ ├─┬ @graphql-tools/[email protected] │ │ │ └── @graphql-tools/[email protected] deduped │ │ └── @graphql-tools/[email protected] deduped │ └── @graphql-tools/[email protected] deduped └─┬ [email protected] ├─┬ @graphql-tools/[email protected] │ └── @graphql-tools/[email protected] deduped └── @graphql-tools/[email protected] deduped