5
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 3 years have passed since last update.

@graphql-codegenの環境変数の設定方法

Posted at

前提使用技術

codegen.yml
overwrite: true

schema:
  - "ここにエンドポイント":
      headers:
        "X-Shopify-Storefront-Access-Token": ${NEXT_PUBLIC_STORE_FRONT_ACCESS_TOKEN}

documents: "src/queries/**/*.ts"
generates:
  src/types/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"

.env
NEXT_PUBLIC_STORE_FRONT_ACCESS_TOKEN="ここにアクセストークンやAPIKEY"

今回はショッピファイ のトークンを想定してました。
headersは自分のアクセスしたいAPIのものを設定します。

.envファイルを読み込みにいくには${}で囲みます。
注意しなくてはいけないのはprocess.envを通常つけるはずですが、この場合は付けなくて良いことです。

package.json
"scripts": {
    "gen-types": "graphql-codegen --require dotenv/config --config codegen.yml"
  }

package.jsonにscriptsを追加します。
--require dotenv/configを設定すると実行時にデフォルトで.envファイルを読みにいきます。

注意点として.envファイルだけで.env.localなどのファイルは読みにいきません。
その場合は
https://www.graphql-code-generator.com/docs/getting-started/require-field
こちらに書いてあるので実践してみましょう!!

お疲れさまでした!

5
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
5
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?