aoudiamoncef / apollo-client-maven-plugin

Generate a Java/Kotlin GraphQL client based on introspection data and predefined queries.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Some type mappings in <customTypeMap /> is ignored

ckwong-jebsen opened this issue · comments

Some of the custom mapping is ignored, for examples, mapping of "Int" is ignored.
Below is my mappings

<customTypeMap>
  <Int>java.lang.Integer</Int>
  <Float>java.math.BigDecimal</Float>
  <Boolean>java.lang.Boolean</Boolean>
  <String>java.lang.String</String>
  <ID>java.lang.String</ID>
  <Decimal>java.math.BigDecimal</Decimal>
  <Money>java.math.BigDecimal</Money>
  <UnsignedInt64>java.math.BigInteger</UnsignedInt64>
  <Date>java.time.ZonedDateTime</Date>
  <DateTime>java.time.ZonedDateTime</DateTime>
  <UtcOffset>java.time.ZoneOffset</UtcOffset>
  <FormattedString>java.lang.String</FormattedString>
  <HTML>java.lang.String</HTML>
  <JSON>java.lang.String</JSON>
  <StorefrontID>java.lang.String</StorefrontID>
  <URL>java.lang.String</URL>
  <ARN>java.lang.String</ARN>
</customTypeMap>

Could you please kindly help?

Hi @ckwong-jebsen
You have to create your own adapter:

       val longCustomTypeAdapter = object : CustomTypeAdapter<Long> {
            override fun encode(value: Long): CustomTypeValue<*> {
                return value.toString() as CustomTypeValue<String>
            }

            override fun decode(value: CustomTypeValue<*>): Long {
                return (value.value as BigDecimal).toLong()
            }
        }
<customTypeMap>
    <DateTime>java.time.LocalDateTime</DateTime>
    <GeoJSONString>java.lang.String</GeoJSONString>
</customTypeMap>

I also encountered the same problem. When it is generating CustomType.java file, it doesn't include the GeoJSONString CustomType.

Here's a snippet of my schema.json file

{
...
},
{
    "kind": "SCALAR",
    "name": "GeoJSONString",
    "description": "A GeoJSON formatted String.\n\n\nCurrently only supports Point GeoJSON type.\n\ne.g.\n\n\"{\n    \"type\": \"Point\",\n    \"coordinates\": [125.6, 10.1]\n}\"\n\n\nSupport for LineString GeoJSON type is under development.\n\n\n\nPlease see https://geojson.org for more info.",
    "fields": null,
    "inputFields": null,
    "interfaces": null,
    "enumValues": null,
     "possibleTypes": null
},
{
...
}

I also tried your suggestion in providing a CustomTypeAdapter but still didn't include the GeoJSONString CustomTypeMap in CustomType.java file upon generation.

public enum CustomType implements ScalarType {
  DATETIME {
    @Override
    public String typeName() {
      return "DateTime";
    }

    @Override
    public String className() {
      return "java.time.LocalDateTime";
    }
  },

  ID {
    @Override
    public String typeName() {
      return "ID";
    }

    @Override
    public String className() {
      return "java.lang.String";
    }
  }
}

Please advise if I did something wrong on my end or recommend how should I be able to fix it.

By the way, appreciate what you did with this plugin, it's a huge lifesaver for one of the projects that I'm involved in.

Hi @dryst0,
have you added adapter's to your ApolloClient instance ?

ApolloClient.builder()
  .serverUrl(serverUrl)
  .addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter)
  .build()

Hi @dryst0,
have you added adapter's to your ApolloClient instance ?

ApolloClient.builder()
  .serverUrl(serverUrl)
  .addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter)
  .build()

Yes, I have also added the custom type adapters to my apolloClient

ApolloClient apolloClient = ApolloClient.builder().serverUrl(impressionsEndpoint)
                .addCustomTypeAdapter(CustomType.DATETIME, dateTimeAdapter)
                .addCustomTypeAdapter(CustomType.GEOJSONSTRING, geojsonCustomTypeAdapter).okHttpClient(client).build();

Only CustomType.DATETIME was included in the auto-generated class file. CustomType.GEOJSONSTRING doesn't exist and therefore my build is failing.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project common: Compilation failure
[ERROR] <redacted> cannot find symbol
[ERROR]   symbol:   variable GEOJSONSTRING
[ERROR]   location: class m.m.m.graphql.client.type.CustomType

I'll try to add a test for this case in next release.

I think that you have to report this issue to the main project which is Apollo Android

It could be an issue with Apollo compiler

thanks

I think that you have to report this issue to the main project which is Apollo Android

Alright. I'll also raise an issue there as well.

Do you have any suggestions to work around this issue in the meanwhile?

I am thinking of copying the generated-sources back to main directory and manually adding the CustomType.GEOJSONSTRING in CustomType.java and commenting out this maven plugin for now until the issue gets resolved.

Thank you also for the quick response.

Just wanted to refer the post-mortem report regarding the issue I've raised here.

apollographql/apollo-kotlin#2721 (comment)

TLDR; Multiple execution section under the same plugin which writes the output on the same directory and overwrites the CustomType.java

Also, thanks for all the help!