Hey, I'm trying to inspect the request when creati...
# graphql-kotlin
Hey, I'm trying to inspect the request when creating a
to be able to customize a callout to an external service, but I can't find how to get hold of the actual query body from the request? Is there a way to get hold of the raw query when creating my context?
assuming you are using
, creation of context happens before parsing of the query
since it executes in a webfilter technically you can read your request payload but I am unsure if thats a good idea
*i’d say you probably should not do it as it is a pretty heavy operation
when resolving the fields you do have access to environment (and context) which you could inspect to trigger different behavior
thanks! what i really need is to know exactly which fields were included in the query - is there an easy way to get hold of that? or do you mean that this is accessible in the environment?
There is a
that is supposed to be cached, but I don't find any payload there
yeah environment will have the info
accessing this information when building out context will be troublesome -> i.e. you will need to read post body (heavy operation that will have to be repeated afterwards anyway) and then parse it (again throwaway work)
you could look into doing some custom query handler that performs context construction (instead of webfilter) but you would still end up parsing query twice
Ah, so maybe it's better to get the environment injected in the Query method then?
*unless you rewrite graphql java logic to use your parsed result
i’d go with environment
that would work great as well - doesn't have to be the context as far as I'm concerned, even if it would look a bit cleaner 🙂 Thanks a lot for the help!
👍 good luck
The other option is using graphql-java instrumentation https://www.graphql-java.com/documentation/v15/instrumentation/ But I think accessing the
in the query is probably best: https://expediagroup.github.io/graphql-kotlin/docs/schema-generator/execution/data-fetching-environment
Thanks! the thing is that I need to know the query before I construct the return type, so unless I'm misunderstanding I can't use a`DataFetchingEnvironmet` to get the query, since that's only applied on getters on fields? I was thinking if it was possible to customize the queryHandler logic so that it inspects the Reactor subscription context and checks if there is a
there, and if the
implements some marker interface like
it injects the constructed query there via some setter. Would that make any sense? I'm a bit new to the project, so would appreciate any feedback 🙂
is available when resolving any field but
can automatically inject it as a function parameter, e.g. your top level query where you can read it and then apply some custom logic based on it (i.e. call X or Y) can you elaborate more on your use case? it sounds like you are trying to do something that you probably should not be doing (or there is a simpler way of doing it)
Sorry, my notifications seem to be wonky, so I didn't see this... My use case is that we're talking to a gRPC service - the GraphQL service would essentially be a proxy to it - and are using field masks when making the request, which means that I do a request for a full object and not making callouts for certain fields. I thought I'd send a PR to describe what would work for me - should be ready quite soon.
Ah, the DataFetcherEnvironment does work now - I must have done something wrong while trying to get it injected before. Thanks again!