Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some type mappings in <customTypeMap /> is ignored #40

Closed
ckwong-jebsen opened this issue Nov 4, 2020 · 7 comments
Closed

Some type mappings in <customTypeMap /> is ignored #40

ckwong-jebsen opened this issue Nov 4, 2020 · 7 comments

Comments

@ckwong-jebsen
Copy link

ckwong-jebsen commented Nov 4, 2020

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?

@aoudiamoncef
Copy link
Owner

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()
            }
        }

@dryst0
Copy link

dryst0 commented Nov 19, 2020

<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.

@aoudiamoncef
Copy link
Owner

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

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

@dryst0
Copy link

dryst0 commented Nov 19, 2020

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

@aoudiamoncef
Copy link
Owner

aoudiamoncef commented Nov 19, 2020

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

@dryst0
Copy link

dryst0 commented Nov 19, 2020

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.

@dryst0
Copy link

dryst0 commented Nov 20, 2020

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants