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

Multiple directives not working in the sequence as defined in the graphql query

Open vasanti-suthar-us opened this issue 2 years ago • 5 comments

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 generate in the terminal.

    Please make sure the GraphQL Tools package versions under package.json matches 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

vasanti-suthar-us avatar Jul 13 '23 14:07 vasanti-suthar-us

Could you share a reproduction on CodeSandbox? Thanks!

ardatan avatar Jul 13 '23 14:07 ardatan

@ardatan please look into the below reproduction on CodeSandbox https://codesandbox.io/s/apollo-server-v4-d5nwml

vasanti-suthar-us avatar Jul 14 '23 09:07 vasanti-suthar-us

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]

wtfiwtz avatar Oct 04 '23 01:10 wtfiwtz

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

wtfiwtz avatar Oct 04 '23 02:10 wtfiwtz

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

fridaystreet avatar Oct 27 '23 11:10 fridaystreet