Distelli / graphql-apigen

Generate Java APIs with GraphQL Schemas
Apache License 2.0
66 stars 20 forks source link

Using graphql-apigen #28

Open gdelafosse opened 6 years ago

gdelafosse commented 6 years ago

Hello I have difficulties to integrate graphql-apigen generated beans with CDI without guice. I use jboss as an application server. Could you provide an exemple?

brimworks commented 6 years ago

Sorry, I'm not familiar with CDI or jboss, so I can't provide any examples.

gdelafosse commented 6 years ago

Ok, we manage to make it work by changing @Inject private Optional _impl

by @Inject private Instance _impl

in the generated TypeProviders.

Using javax.enterprise.inject.Instance is supported by the jboss CDI implementation. Hope we could provide a pull request.

brimworks commented 6 years ago

Humm... it sounds like javax.enterprise.inject.Instance is a CDI specific thing. It might be worth commenting on this:

https://issues.jboss.org/browse/CDI-45?_sscc=t

...perhaps a good compromise is for graphql-apigen to generate CDI specific TypeProviders?

Note that if you want to avoid forking graphql-apigen, you can simply copy the modified graphql-apigen.stg into the directory containing your pom.xml:

https://github.com/Distelli/graphql-apigen/blob/master/apigen/src/main/resources/graphql-apigen.stg

Thanks, -Brian

gdelafosse commented 6 years ago

Could you explain why those dependencies are optional?

brimworks commented 6 years ago

The optional injection points are for resolve an object id into a full object. This isn't necessary for all objects. For example, you might not have foreign key references to an object type and thus it would never need to be resolved from id to full object.

gdelafosse commented 6 years ago

That's the role of *.Resolver, but what about the _impl? In your example, AuthorTypeProvider needs an Author.Resolver implementation and also an Author implementation. In this case, the role of this Author imlementation. Is it an Author.Impl that will be injected? must I provide a bean that implement Author?

brimworks commented 6 years ago

It's been awhile since I've thought about this, but you can see MethodDataFetcher will use the graphql.schema.DataFetchingEnvironment "source" if impl is null:

https://github.com/Distelli/graphql-apigen/blob/master/apigen-deps/src/main/java/com/distelli/graphql/MethodDataFetcher.java

I believe this is useful if your implementation is a simple POJO that doesn't need to interact with your dependency injection system.

gdelafosse commented 6 years ago

I debugged the Spring version. The AuthorTypeProvider has an Author _impl = null. The Optional injection works in Spring. In my CDI context, I still did not manage to inject an optional dependency. That's why I still have to implement an AuthorImpl that implements @Override public Author resolve(DataFetchingEnvironment env) { return env.getSource(); }

Could you confirm that a Impl MUST be implemented ONLY for types that have function in the schema?

brimworks commented 6 years ago

Could you confirm that a Impl MUST be implemented ONLY for types that have function in the schema?

That is correct.