Hey, I'm trying to inspect the request when creati...
# graphql-kotlin
h
Hey, I'm trying to inspect the request when creating a
GraphQLContext
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?
d
assuming you are using
graphql-kotlin-spring-server
, 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
h
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
request.formData
that is supposed to be cached, but I don't find any payload there
d
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
h
Ah, so maybe it's better to get the environment injected in the Query method then?
d
*unless you rewrite graphql java logic to use your parsed result
yep
i’d go with environment
h
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!
d
👍 good luck
s
The other option is using graphql-java instrumentation https://www.graphql-java.com/documentation/v15/instrumentation/ But I think accessing the
DataFetchingEnvironment
in the query is probably best: https://expediagroup.github.io/graphql-kotlin/docs/schema-generator/execution/data-fetching-environment
h
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
GraphQlContext
there, and if the
GraphQlContext
implements some marker interface like
GraphQlRequestAware
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 🙂
d
DataFetchingEnvironment
is available when resolving any field but
graphql-kotlin
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)
h
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!