AWS Amplify CLIで、.graphqlconfig.ymlを使って、schema.jsonやqueries.jsonなどの保存場所を変更する

Amplify CLIamplify push することで、必要に応じてAppSyncのスキーマファイル ( <project_root>/amplify/backend/api/"api_name"/schema.graphql ) を元に、Amplify Framework向けのソースコードを生成してくれます。

$ amplify push

Current Environment: dev

| Category | Resource name    | Operation | Provider plugin   |
| -------- | ---------------- | --------- | ----------------- |
| Api      | graphqlconfigApi | Create    | awscloudformation |
? Are you sure you want to continue? Yes

GraphQL schema compiled successfully.

? Do you want to generate code for your newly created GraphQL API Yes
? Choose the code generation language target javascript
? Enter the file name pattern of graphql queries, mutations and subscriptions src/graphql/**/*.js

# 自動生成するか
? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions Yes

? Enter maximum statement depth [increase from default if your schema is deeply nested] 2

...
✔ Downloaded the schema
✔ Generated GraphQL operations successfully and saved at src/graphql
✔ All resources are updated in the cloud

 
デフォルトでは、 src/graphql ディレクトリに作成されます。

ただ、amplify push後に、自動生成ファイルの保存先を変更しようと思ったため、その時のことをメモします。

 

環境

  • Amplify CLI 1.8.5

 

.graphqlconfig.ymlを使った修正

.graphqlconfig.yml ファイルについて、公式ドキュメントでは

When a project is configured to generate code with codegen, it stores all the configuration .graphqlconfig.yml file in the root folder of your project.

https://aws-amplify.github.io/docs/cli-toolchain/graphql#codegen

と記載されています。

ただ、どの項目を修正することで保管場所が変更になるか分からなかったため、いろいろと試してみました。

 

queries.json/mutations.json/subscriptions.jsonの移動

GraphQLに対応したファイル

を移動するには、項目 docsFilePath を修正します。

試しに、docsFilePathsrc_2nd/graphql へと変更し、 schema.graphql を修正した後、 amplify push してみました。

$ amplify push

Current Environment: dev

| Category | Resource name    | Operation | Provider plugin   |
| -------- | ---------------- | --------- | ----------------- |
| Api      | graphqlconfigApi | Update    | awscloudformation |
? Are you sure you want to continue? Yes

GraphQL schema compiled successfully.

? Do you want to update code for your updated GraphQL API Yes
? Do you want to generate GraphQL statements (queries, mutations and subscription) based on your schema types? This will overwrite your current graphql queries, mutations and subs
criptions Yes

...
✔ Downloaded the schema
✔ Generated GraphQL operations successfully and saved at src_2nd/graphql
✔ All resources are updated in the cloud

 
すると、

の3ファイルが src_2nd/graphql の中に作成されました。
https://github.com/thinkAmi-sandbox/AWS_AppSync_Amplify-sample/commit/d4658252211362d35296110fdef8b19b507d4e08#diff-65fd0b65d8da646fd955c3e7deaee9b8

 

shema.jsonの移動

shema.json の場合は、項目 schemaPath を修正します。あとは amplify push することで、新しい場所に作成されます。
https://github.com/thinkAmi-sandbox/AWS_AppSync_Amplify-sample/commit/6efb091d1a9d2891cccefe56565c6e05a99019c4#diff-65fd0b65d8da646fd955c3e7deaee9b8

 
ちなみに、 Amplify CLIのバージョンが古い場合はschema.jsonの移動はできません。

 
もし移動したい場合は、最新のAmplify CLIにアップデートする必要があります(手元だと、1.8.5で移動可能でした)。

 

ソースコード

GitHubに上げました。 graphqlconfig_yml ディレクトリの中が今回のファイルです。
https://github.com/thinkAmi-sandbox/AWS_AppSync_Amplify-sample