Help us understand the problem. What is going on with this article?

apollo-androidでGraphQLのCustom scalar typesのマッピングを定義する

More than 1 year has passed since last update.

Custom scalar types

GraphQLスキーマ言語のスカラ型で組み込み型(Int, Float, String, Boolean, ID)以外の型はCustom scalar typesとして扱われます。GitHub GraphQL API v4での例をあげると以下のクエリのavatarUrlURI!型を返します。(ドキュメントはこちら)

query ShowLoginUser {
  viewer {
    avatarUrl
  }
}

apollo-androidでのCustom scalar typesの扱い

apollo-androidでは、Custom scalar typesを「CustomType」として扱います。先ほど例にあげたavatarUrlは、apollo-androidによってデフォルトではjava.lang.Objectにマッピングされます。この際次のような例外メッセージが表示されます。

Can't map GraphQL type: URI to: class java.lang.Object. Did you forget to add custom type adapter?

この問題の解決には、https://github.com/apollographql/apollo-android#custom-scalar-typesが参考になります。今回の場合、次の定義をモジュールレベルのbuild.gradleに定義すればOKです。

app/build.gradle
apollo {
    customTypeMapping = [
            "URI": "java.lang.String"
    ]
}

また、Date型のようなCustomTypeの場合、build.gradleへの定義に加えて、CustomTypeAdapterを定義する必要があります。

参考資料

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away