Understanding the critical parts of wiring custom directives #1576
Replies: 1 comment
-
Hello 👋
Do you mean conditionally resolve the fields? Then yeah, you could do that using the approach above with auth directives - I'd also recommend to make those fields nullable as otherwise attempting to fetch unauthorized field will blow up the whole query. If you need to expose dynamic schema to the end user, i.e. so they don't even see the extra fields, then it gets a somewhat more complex. I'd assume you would need to create a custom spring server that has multiple GraphQL schemas corresponding to the user authentication state (e.g. anonymous read only, user with some additional fields, admin with all fields, etc) and route requests to the appropriate schema based on the user state. As for building multiple instances of schema -> you could start with a single admin one and then transform it to exclude fields (see field visibility). Note: we don't have a built-in mechanism for this scenario so you would need a custom server and implement custom |
Beta Was this translation helpful? Give feedback.
-
I'm working on a project that's already successfully generating a GraphQL schema from Kotlin in a Spring WebFlux environment. I've been tasked with writing a custom directive that uses authorization to conditionally show annotated fields, which seems like a pretty common use case from the discussion I've seen about it here. I found this conversation very helpful in confirming the direction I'm working in
I've managed to create a custom data fetcher, directive, and wiring, and publish it in a library. And when I use it in a project, I can plainly see in the generated schema that the directive is being included. But I can't see any proof that the custom DataFetcher is being executed. In an effort to make sure that it is, I've added logging to the DataFetcher and made it always return null for whatever field is using the directive. However, I never see log output, and the fields still return a valid value when I call the query.
What I think I'm struggling with is the fact that every example I've seen for writing a custom directive explicitly calls
toSchema()
in some way. If the project I'm working on generates its schema by specifying thegraphql.packages
property, and annotating the types, how should I change my project's configuration?I've seen this example which specifies a number of different beans, but it's not clear which beans I need if I'm only trying to include a custom directive. Do I just need
wiringFactory
? OrwiringFactory
andhooks
specified?Here's what I've setup.
Config
CustomDirectiveWiringFactory
Directive
DataFetcher
(Note, for testing purposes, that this is currently setup to always return a null value)
Beta Was this translation helpful? Give feedback.
All reactions