LoginSignup
4
3

More than 5 years have passed since last update.

Apollo Clientで__typenameの自動生成を無効にする方法について

Posted at

AngularでApllo Clientを使っていて、typenameが意図しないものになる事があります。
こうなるとGraphqlQLの呼び出しが失敗します。
意図した
typenameを付ける方法を調べたのですが分かりませんでした。

対象のコード

Angular側では、graphql-tagを使って以下のように定義しています。

sample.ts
  private readonly saveSiteConfigMutation = gql`
    mutation savesiteconfig($savesiteconfig: SaveSiteConfig!) {
      savesiteconfig(savesiteconfig: $savesiteconfig) {
        result
        message
      }
    }
  `;

期待としては、__typenameにはSaveSiteConfigが設定されて欲しいのですが、何故だかSiteConfigとなってしまいます。

回避方法

そもそもtypenameは必要ないんだよなということで調べて見たら、https://github.com/apollographql/apollo-client/issues/1913 を見つけました。
希望通り、
typenameを付けない方法です。

sample.ts
    apollo.create({
      link,
      cache: new InMemoryCache({
        addTypename: false
      })
    });

これが正解なのか分かりませんが、問題は解決しました。

令和最初のQiita投稿でした。

4
3
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
4
3