Skip to content

Commit

Permalink
feat: allow multiple graphql APIs in a cdk app (aws-amplify#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch authored Apr 19, 2024
1 parent cd6b1f5 commit 72ca6ba
Show file tree
Hide file tree
Showing 25 changed files with 368 additions and 163 deletions.
4 changes: 2 additions & 2 deletions packages/amplify-data-construct/.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -3520,7 +3520,7 @@
},
"name": "@aws-amplify/data-construct",
"readme": {
"markdown": "# AmplifyData Construct\n\n[![View on Construct Hub](https://constructs.dev/badge?package=%40aws-amplify%2Fdata-construct)](https://constructs.dev/packages/@aws-amplify/data-construct)\n\nThis package vends an L3 CDK Construct wrapping the behavior of the Amplify GraphQL Transformer. This enables quick development and interation of AppSync APIs which support the Amplify GraphQL Directives. For more information on schema modeling in GraphQL, please refer to the [amplify developer docs](https://docs.amplify.aws/cli/graphql/overview/).\n\nThe primary way to use this construct is to invoke it with a provided schema (either as an inline graphql string, or as one or more `appsync.SchemaFile`) objects, and with authorization config provided. There are 5 supported methods for authorization of an AppSync API, all of which are supported by this construct. For more information on authorization rule definitions in Amplify, refer to the [authorization docs](https://docs.amplify.aws/cli/graphql/authorization-rules/). Note: currently at least one authorization rule is required, and if multiple are specified, a `defaultAuthorizationMode` must be specified on the api as well. Specified authorization modes must be a superset of those configured in the graphql schema.\n\nNote: only a single instance of the `AmplifyData` construct can be invoked within a CDK synthesis at this point in time.\n\n## Examples\n\n### Simple Todo List With Cognito Userpool-based Owner Authorization\n\nIn this example, we create a single model, which will use `user pool` auth in order to allow logged in users to create and manage their own `todos` privately.\n\nWe create a cdk App and Stack, though you may be deploying this to a custom stack, this is purely illustrative for a concise demo.\n\nWe then wire this through to import a user pool which was already deployed (creating and deploying is out of scope for this example).\n\n```ts\nimport { App, Stack } from 'aws-cdk-lib';\nimport { UserPool } from 'aws-cdk-lib/aws-cognito';\nimport { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';\n\nconst app = new App();\nconst stack = new Stack(app, 'TodoStack');\n\nnew AmplifyData(stack, 'TodoApp', {\n definition: AmplifyDataDefinition.fromString(/* GraphQL */ `\n type Todo @model @auth(rules: [{ allow: owner }]) {\n description: String!\n completed: Boolean\n }\n `),\n authorizationModes: {\n userPoolConfig: {\n userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),\n },\n },\n});\n```\n\n### Multiple related models, with public read access, and admin read/write access\n\nIn this example, we create a two related models, which will use which logged in users in the 'Author' and 'Admin' user groups will have\nfull access to, and customers requesting with api key will only have read permissions on.\n\n```ts\nimport { App, Stack } from 'aws-cdk-lib';\nimport { UserPool } from 'aws-cdk-lib/aws-cognito';\nimport { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';\n\nconst app = new App();\nconst stack = new Stack(app, 'BlogStack');\n\nnew AmplifyData(stack, 'BlogApp', {\n definition: AmplifyDataDefinition.fromString(/* GraphQL */ `\n type Blog @model @auth(rules: [{ allow: public, operations: [read] }, { allow: groups, groups: [\"Author\", \"Admin\"] }]) {\n title: String!\n description: String\n posts: [Post] @hasMany\n }\n\n type Post @model @auth(rules: [{ allow: public, operations: [read] }, { allow: groups, groups: [\"Author\", \"Admin\"] }]) {\n title: String!\n content: [String]\n blog: Blog @belongsTo\n }\n `),\n authorizationModes: {\n defaultAuthorizationMode: 'API_KEY',\n apiKeyConfig: {\n description: 'Api Key for public access',\n expires: cdk.Duration.days(7),\n },\n userPoolConfig: {\n userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),\n },\n },\n});\n```\n\n### Import GraphQL Schema from files, instead of inline.\n\nIn this example, we import the schema definition itself from one or more local file, rather than an inline graphql string.\n\n```graphql\n# todo.graphql\ntype Todo @model @auth(rules: [{ allow: owner }]) {\n content: String!\n done: Boolean\n}\n```\n\n```graphql\n# blog.graphql\ntype Blog @model @auth(rules: [{ allow: owner }, { allow: public, operations: [read] }]) {\n title: String!\n description: String\n posts: [Post] @hasMany\n}\n\ntype Post @model @auth(rules: [{ allow: owner }, { allow: public, operations: [read] }]) {\n title: String!\n content: [String]\n blog: Blog @belongsTo\n}\n```\n\n```ts\n// app.ts\nimport { App, Stack } from 'aws-cdk-lib';\nimport { UserPool } from 'aws-cdk-lib/aws-cognito';\nimport { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';\n\nconst app = new App();\nconst stack = new Stack(app, 'MultiFileStack');\n\nnew AmplifyData(stack, 'MultiFileDefinition', {\n definition: AmplifyDataDefinition.fromFiles(path.join(__dirname, 'todo.graphql'), path.join(__dirname, 'blog.graphql')),\n authorizationModes: {\n defaultAuthorizationMode: 'API_KEY',\n apiKeyConfig: {\n description: 'Api Key for public access',\n expires: cdk.Duration.days(7),\n },\n userPoolConfig: {\n userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),\n },\n },\n});\n```\n"
"markdown": "# AmplifyData Construct\n\n[![View on Construct Hub](https://constructs.dev/badge?package=%40aws-amplify%2Fdata-construct)](https://constructs.dev/packages/@aws-amplify/data-construct)\n\nThis package vends an L3 CDK Construct wrapping the behavior of the Amplify GraphQL Transformer. This enables quick development and interation of AppSync APIs which support the Amplify GraphQL Directives. For more information on schema modeling in GraphQL, please refer to the [amplify developer docs](https://docs.amplify.aws/cli/graphql/overview/).\n\nThe primary way to use this construct is to invoke it with a provided schema (either as an inline graphql string, or as one or more `appsync.SchemaFile`) objects, and with authorization config provided. There are 5 supported methods for authorization of an AppSync API, all of which are supported by this construct. For more information on authorization rule definitions in Amplify, refer to the [authorization docs](https://docs.amplify.aws/cli/graphql/authorization-rules/). Note: currently at least one authorization rule is required, and if multiple are specified, a `defaultAuthorizationMode` must be specified on the api as well. Specified authorization modes must be a superset of those configured in the graphql schema.\n\n## Examples\n\n### Simple Todo List With Cognito Userpool-based Owner Authorization\n\nIn this example, we create a single model, which will use `user pool` auth in order to allow logged in users to create and manage their own `todos` privately.\n\nWe create a cdk App and Stack, though you may be deploying this to a custom stack, this is purely illustrative for a concise demo.\n\nWe then wire this through to import a user pool which was already deployed (creating and deploying is out of scope for this example).\n\n```ts\nimport { App, Stack } from 'aws-cdk-lib';\nimport { UserPool } from 'aws-cdk-lib/aws-cognito';\nimport { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';\n\nconst app = new App();\nconst stack = new Stack(app, 'TodoStack');\n\nnew AmplifyData(stack, 'TodoApp', {\n definition: AmplifyDataDefinition.fromString(/* GraphQL */ `\n type Todo @model @auth(rules: [{ allow: owner }]) {\n description: String!\n completed: Boolean\n }\n `),\n authorizationModes: {\n userPoolConfig: {\n userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),\n },\n },\n});\n```\n\n### Multiple related models, with public read access, and admin read/write access\n\nIn this example, we create a two related models, which will use which logged in users in the 'Author' and 'Admin' user groups will have\nfull access to, and customers requesting with api key will only have read permissions on.\n\n```ts\nimport { App, Stack } from 'aws-cdk-lib';\nimport { UserPool } from 'aws-cdk-lib/aws-cognito';\nimport { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';\n\nconst app = new App();\nconst stack = new Stack(app, 'BlogStack');\n\nnew AmplifyData(stack, 'BlogApp', {\n definition: AmplifyDataDefinition.fromString(/* GraphQL */ `\n type Blog @model @auth(rules: [{ allow: public, operations: [read] }, { allow: groups, groups: [\"Author\", \"Admin\"] }]) {\n title: String!\n description: String\n posts: [Post] @hasMany\n }\n\n type Post @model @auth(rules: [{ allow: public, operations: [read] }, { allow: groups, groups: [\"Author\", \"Admin\"] }]) {\n title: String!\n content: [String]\n blog: Blog @belongsTo\n }\n `),\n authorizationModes: {\n defaultAuthorizationMode: 'API_KEY',\n apiKeyConfig: {\n description: 'Api Key for public access',\n expires: cdk.Duration.days(7),\n },\n userPoolConfig: {\n userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),\n },\n },\n});\n```\n\n### Import GraphQL Schema from files, instead of inline.\n\nIn this example, we import the schema definition itself from one or more local file, rather than an inline graphql string.\n\n```graphql\n# todo.graphql\ntype Todo @model @auth(rules: [{ allow: owner }]) {\n content: String!\n done: Boolean\n}\n```\n\n```graphql\n# blog.graphql\ntype Blog @model @auth(rules: [{ allow: owner }, { allow: public, operations: [read] }]) {\n title: String!\n description: String\n posts: [Post] @hasMany\n}\n\ntype Post @model @auth(rules: [{ allow: owner }, { allow: public, operations: [read] }]) {\n title: String!\n content: [String]\n blog: Blog @belongsTo\n}\n```\n\n```ts\n// app.ts\nimport { App, Stack } from 'aws-cdk-lib';\nimport { UserPool } from 'aws-cdk-lib/aws-cognito';\nimport { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';\n\nconst app = new App();\nconst stack = new Stack(app, 'MultiFileStack');\n\nnew AmplifyData(stack, 'MultiFileDefinition', {\n definition: AmplifyDataDefinition.fromFiles(path.join(__dirname, 'todo.graphql'), path.join(__dirname, 'blog.graphql')),\n authorizationModes: {\n defaultAuthorizationMode: 'API_KEY',\n apiKeyConfig: {\n description: 'Api Key for public access',\n expires: cdk.Duration.days(7),\n },\n userPoolConfig: {\n userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),\n },\n },\n});\n```\n"
},
"repository": {
"directory": "packages/amplify-data-construct",
Expand All @@ -3535,5 +3535,5 @@
},
"types": {},
"version": "1.7.2",
"fingerprint": "lhTH8On2vgQ1f/2zsM4RwATDNauPbYxMg3txq5pGaMU="
"fingerprint": "4gdnEpjS4yQg2de7yrDbsRfoYCPl/SorshNUpdweEho="
}
2 changes: 0 additions & 2 deletions packages/amplify-data-construct/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ This package vends an L3 CDK Construct wrapping the behavior of the Amplify Grap

The primary way to use this construct is to invoke it with a provided schema (either as an inline graphql string, or as one or more `appsync.SchemaFile`) objects, and with authorization config provided. There are 5 supported methods for authorization of an AppSync API, all of which are supported by this construct. For more information on authorization rule definitions in Amplify, refer to the [authorization docs](https://docs.amplify.aws/cli/graphql/authorization-rules/). Note: currently at least one authorization rule is required, and if multiple are specified, a `defaultAuthorizationMode` must be specified on the api as well. Specified authorization modes must be a superset of those configured in the graphql schema.

Note: only a single instance of the `AmplifyData` construct can be invoked within a CDK synthesis at this point in time.

## Examples

### Simple Todo List With Cognito Userpool-based Owner Authorization
Expand Down
40 changes: 20 additions & 20 deletions packages/amplify-graphql-api-construct/.jsii

Large diffs are not rendered by default.

Loading

0 comments on commit 72ca6ba

Please sign in to comment.