Distelli / graphql-apigen

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

Add support for custom model addapters #34

Open kito99 opened 5 years ago

kito99 commented 5 years ago

I'm customizing the template in order to change how the Java code is generated. However, there are a couple of things I'd like to do that can't be done without modifying STDModel.java (which I would like to avoid doing). I noticed that one way around this is to use StringTemplate model adaptor (https://github.com/antlr/stringtemplate4/blob/master/doc/adaptors.md). It'd be great if this project had support for adding custom model generators; it would make it quite extensible.

brimworks commented 5 years ago

I'm not following on how model adaptors would be useful. Do you have some example code?

kito99 commented 5 years ago

I don't have example code, but the scenario is pretty simple. I'm just trying to determine if a particular type is a Query. I don't see that information in the GraphQL model for the AST, but I could do something silly like check the name of the type or look for a special comment. However, since StringTemplate doesn't allow arbitrary expressions, the only option is to call a method on STDModel that returns a boolean value. Rather than modifying the class directly, if I could register a model adapter, I could add a new boolean value like isQuery that I could use in the template.

brimworks commented 5 years ago

Ah, I think I understand... do you want to add an isQuery() method to the STModel? We always appreciate pull requests :).

kito99 commented 5 years ago

Well, adding the model adapter would allow extending the model in ways that aren't generally useful to other teams. In this case, though, is there any way to tell if a type is a query other than looking at the name or a comment?