放大数据构造
此套件会 vend a L3 CDK Constructure 包裝了 Amplify GraphQL 变形器的行为. 这使得AppSync API能够快速开发并插入支持"放大图形QL指令". 关于GraphQL中计划模型的更多信息,请参见: 扩展开发者文件。 。 。 .
使用此构造的主要方式是用一个提供的 schema( 要么作为内置的图克字符串, 要么作为一个或多个) 引用它 appsync.SchemaFile(a) 物品,并附有授权配置。 AppSync API的授权有5个支持的方法,这些都由这个构造支持. 关于授权规则定义的更多信息,请参见: 授权文件注:目前至少需要一项授权规则,如果有多项授权,则需一项授权规则 defaultAuthorizationMode 必须在相片上指定。 指定的授权模式必须是在图克l schema中配置的这些模式的超级集.
实例
以 Cognitto 用户池为基础的所有者授权的简单待办事宜列表
在这个例子中,我们创建一个单一的模型,它将使用 user pool 认证以允许用户登录以创建和管理自己的 todos 私下.
我们创建了 Cdk App 和 Stack, 尽管您可能正在将它应用到自定义的堆栈中, 这纯粹是一个简洁的演示.
然后,我们通过电线导入一个已经部署的用户库(例如,创建和部署已超出范围).
import { App, Stack } from 'aws-cdk-lib';
import { UserPool } from 'aws-cdk-lib/aws-cognito';
import { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';
const app = new App();
const stack = new Stack(app, 'TodoStack');
new AmplifyData(stack, 'TodoApp', {
definition: AmplifyDataDefinition.fromString(/* GraphQL */ `
type Todo @model @auth(rules: [{ allow: owner }]) {
description: String!
completed: Boolean
}
`),
authorizationModes: {
userPoolConfig: {
userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),
},
},
});
多个相关模式,可公开阅读访问,管理员阅读/写入访问
在这个例子中,我们创建了两个相关的模型,这些模型将在“Author”和“Admin”用户组的用户中登录 完全访问,使用 api 密钥请求的客户只能读取权限 .
import { App, Stack } from 'aws-cdk-lib';
import { UserPool } from 'aws-cdk-lib/aws-cognito';
import { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';
const app = new App();
const stack = new Stack(app, 'BlogStack');
new AmplifyData(stack, 'BlogApp', {
definition: AmplifyDataDefinition.fromString(/* GraphQL */ `
type Blog @model @auth(rules: [{ allow: public, operations: [read] }, { allow: groups, groups: ["Author", "Admin"] }]) {
title: String!
description: String
posts: [Post] @hasMany
}
type Post @model @auth(rules: [{ allow: public, operations: [read] }, { allow: groups, groups: ["Author", "Admin"] }]) {
title: String!
content: [String]
blog: Blog @belongsTo
}
`),
authorizationModes: {
defaultAuthorizationMode: 'API_KEY',
apiKeyConfig: {
description: 'Api Key for public access',
expires: cdk.Duration.days(7),
},
userPoolConfig: {
userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),
},
},
});
从文件导入 GraphQL Schema, 而不是内含 .
在这个例子中,我们从一个或多个本地文件中导入了 schema 定义本身,而不是内置的图克字符串.
# todo.graphql
type Todo @model @auth(rules: [{ allow: owner }]) {
content: String!
done: Boolean
}
# blog.graphql
type Blog @model @auth(rules: [{ allow: owner }, { allow: public, operations: [read] }]) {
title: String!
description: String
posts: [Post] @hasMany
}
type Post @model @auth(rules: [{ allow: owner }, { allow: public, operations: [read] }]) {
title: String!
content: [String]
blog: Blog @belongsTo
}
// app.ts
import { App, Stack } from 'aws-cdk-lib';
import { UserPool } from 'aws-cdk-lib/aws-cognito';
import { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct';
const app = new App();
const stack = new Stack(app, 'MultiFileStack');
new AmplifyData(stack, 'MultiFileDefinition', {
definition: AmplifyDataDefinition.fromFiles(path.join(__dirname, 'todo.graphql'), path.join(__dirname, 'blog.graphql')),
authorizationModes: {
defaultAuthorizationMode: 'API_KEY',
apiKeyConfig: {
description: 'Api Key for public access',
expires: cdk.Duration.days(7),
},
userPoolConfig: {
userPool: UserPool.fromUserPoolId(stack, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'),
},
},
});