smallrye / smallrye-graphql

Implementation for MicroProfile GraphQL
Apache License 2.0
160 stars 91 forks source link

Question/Discussion: GraphQL Endpoint handling/parsing incoming HttpRequest headers #672

Closed janknobloch closed 3 years ago

janknobloch commented 3 years ago

Hi all, im using a quarkus smallrye-graphql project and was wondering whats the best approach to parse header from the HTTPRequest (Post) incoming via the @GraphQLEndpoint and @Queryannotations respectively.

After some reading through different Issues in and out of smallrye, im uncertain on how to implement the needed functionality which is "simply" accessing send http request headers.

I found some discussion of you @phillip-kruger about the: @Context annotation which seems to give you an experimental context (not containing any http specific information) which seems to be also an ongoing discussion:

https://github.com/eclipse/microprofile-graphql/issues/127

Using the Context you provide it seems its still not possible to get the actual HTTPRequest and therefore the headers. Or do I miss an important cast / unwrap?

Then I found that graphl-java switched their GraphQLContext to split them up in two Flavors and therefore providing the underlying HTTPRequest: https://github.com/graphql-java-kickstart/graphql-java-servlet/releases/tag/v8.0.0 GraphQLServletContext and GraphQLWebSocketContext,

I also saw the client side implementation allowing annotations for @Header on the Smallrye Client Side by @t1 and comparing the behaviour to Resteasy which handles Headers using @HeaderParam in a similar manner.

What would be the best approach (for now) accessing the headers for me?

@Inject
Context context;

//inside querymethod:
 DataFetchingEnvironment env = context.unwrap(DataFetchingEnvironment.class);
//which kind of cast or access should I use here ? or should i pass some annotation towards my query methods?

Thanks for your support!

phillip-kruger commented 3 years ago

This is a runtime issue, as runtimes can implement the transport layer themself and this is not mandated by the spec.

In Quarkus, the HTTP Transport is build on Vertx, so the easiest way to do this in Quarkus is to inject the Vertx RoutingContext:

@Inject
RoutingContext routingContext;

Then you can look at the headers anywhere:

String header = routingContext.request().getHeader("accept");

Let me know if this works for you.

janknobloch commented 3 years ago

Thanks for pointing this out @phillip-kruger and also for the super quick reply.

Tested and looks good, sorry for bothering with a non specific graphql issue.

Ill close this, but maybe this helps other ppl around if they find this issue.

phillip-kruger commented 3 years ago

No problem at all. Yes might be good to document this in the Quarkus GraphQL Guide.

I also updated my example (https://github.com/phillip-kruger/graphql-example) see https://github.com/phillip-kruger/graphql-example/blob/e4e4449bfe1b36691fe2861378091a7d1a9e015b/quarkus-example/src/main/java/com/github/phillipkruger/user/graphql/PersonGraphQLApi.java#L31

And now with the latest UI you can add headers in GraphQL UI:

image

So above will add a header called bla

janknobloch commented 3 years ago

very nice indeed. much love!

t1 commented 3 years ago

Maybe we should consider a way to access headers in a portable way? That would feel more like the JEE/MP way.