2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-01-18

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を定義する必要があります。

参考資料

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?