1
1

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 codegenの型定義生成メモ

Last updated at Posted at 2020-04-02

apollo install

2020/04/02時点ではapollo 2.25.0であった。

$ yarn add apollo

apollo.config.js

includes には、Query を書いているファイルを glob で指定する。
型定義の他、VSCodeの拡張等がapollo.client.jsを読んでqueryの補完等を行ってくれる。

Apollo schema registry を使用する場合

module.exports = {
  client: {
    includes: ["./src/**/*.tsx", "./src/**/*.ts"],
    service: "my-apollo-service"
  }
};

エンドポイントからスキーマをリンクする場合

module.exports = {
  client: {
    includes: ["./src/**/*.tsx", "./src/**/*.ts"],
    service: {
      name: "service-name",
      url: "https://sample.com/graphql/"
    }
  }
};

ローカルファイルからスキーマをリンクする場合

module.exports = {
  client: {
    includes: ["./src/**/*.tsx", "./src/**/*.ts"],
    service: {
      name: "service-name",
      localSchemaFile: "./path/to/schema.graphql"
    }
  }
};

コマンド実行

{
  "scripts": {
    "codegen": "apollo client:codegen src/types/api.d.ts --useReadOnlyTypes --addTypename --target=typescript --outputFlat"
  }
}
yarn run codegen

参考

Configuring Apollo projects - Apollo Basics - Apollo GraphQL Docs

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?