How can I get custom directive on input field? #617
-
Hello, is it possiable with dgs to check if there is a custom directive is set to input field? Schema example: directive @hasRole(role: String!) on INPUT_FIELD_DEFINITION
mutation {
updateUser(id ID!, input: UpdateUserInput) : User
}
type User {
id: ID!
name: String
role: String
}
input UpdateUserInput {
name: String
role: String @hasRole("admin")
}
Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
There is no special logic in the DGS framework that is needed to support this functionality. If it is possible to do this per spec (and seems to be the case here: https://spec.graphql.org/June2018/#sec-Language.Directives), it should work. That said, I have not tried this myself. I assume you will need be adding specific logic to handle the directive and so you will need to check if you have everything you need at that point. |
Beta Was this translation helpful? Give feedback.
-
Here is a good reference describing how to set up custom directives:
https://www.graphql-java.com/documentation/v11/sdl-directives/
…On Thu, Sep 9, 2021 at 12:15 PM Maxim Savenko ***@***.***> wrote:
Thank you for your reply. Actually I'm new to java graphql-java and dgs,
and unfortunately I couldn't find any example on this case. I have no idea
where I should put my logic to check directives on input fields.
Could you give me some clue, where I should start with adding logic to
test the directives?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#617 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJ5JPXNZMKM3B35IN3C5STTUBEBUTANCNFSM5DWN5CVA>
.
|
Beta Was this translation helpful? Give feedback.
-
I assume you will have to do the transformation within the data fetcher for
the query that uses the input type? Again, I haven't tried this since
myself, but I assume you have access to the input arguments and can use
that to do the transformation. If not, it's probably not supported by
graphql-java to begin with.
…On Sat, Sep 11, 2021 at 6:23 AM Maxim Savenko ***@***.***> wrote:
Thanks for the link. But it is all about directives that are set on type
fields (FIELD_DEFINITION).
It's pretty clear how to do it - just wrap one datafetcher with a new one
where we can put additional behavior.
The problem is I couldn't apply this approach to input fields
(INPUT_FIELD_DEFINITION).
I understand that I have to override onInputObjectField method on my
Custom Directive class like:
class HasRoleDirective : SchemaDirectiveWiring {
override fun onInputObjectField(environment: SchemaDirectiveWiringEnvironment<GraphQLInputObjectField>): GraphQLInputObjectField {
// Put some code that will be run when input object is passed
// How to do this?
}
}
Maybe I'm wrong, but I think that there are no datafetchers for input
objects and nothing to be wrapped as with regular fields. So how can I
dynamically transform my input object fields?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#617 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJ5JPXLSDNRU4JMC6DFPERLUBNJ5XANCNFSM5DWN5CVA>
.
|
Beta Was this translation helpful? Give feedback.
-
I am not sure I understand the actual use case. That said, I just I tried overriding the
Is that what you are trying to do? What is the objective of setting the directive on the input field? |
Beta Was this translation helpful? Give feedback.
-
Actually the validation library provided by graphql-java does what you are looking for: https://github.com/graphql-java/graphql-java-extended-validation/blob/master/src/main/java/graphql/validation/schemawiring/ValidationSchemaWiring.java#L34 You could probably use that approach. Essentially, you will need to override the The extended-validation library does something similar and builds a validation environment with all the necessary information from the DataFetchingEnvironment. Hope that provides some pointers. |
Beta Was this translation helpful? Give feedback.
Actually the validation library provided by graphql-java does what you are looking for: https://github.com/graphql-java/graphql-java-extended-validation/blob/master/src/main/java/graphql/validation/schemawiring/ValidationSchemaWiring.java#L34
You could probably use that approach. Essentially, you will need to override the
onField
method in SchemaDirectiveWiring and replace the data fetcher. Unfortunately, you will need to dig into the data fetching environment to extract the input value definitions and check which ones have the directive applied to them to customize the logic. Don't believe there is an easier way around this.The extended-validation library does something similar and buil…