def __init__( self, name: str, amplify_api_name: Input[str], graphql_types: Input[List[str]], user_pool: Input[cognito.UserPool], user_pool_client: Input[cognito.UserPoolClient], client_source_path: Input[str] = "src", opts=None, ): """ :param str name: The Pulumi name for this resource :param str api_name: The name of the API in Amplify :param List[str] graphql_types: The names of the GraphQL types, as defined in the schema, for which resolvers should be generated :param user_pool: The Pulumi resource for the Cognito User Pool to be used for authenticating GraphQL requests :param user_pool_client: The Pulumi resource for the Cognito User Pool client :param client_source_path: The path to the client source directory. This is where the `aws-exports.js` file will be placed. """ super().__init__("nuage:aws:AmplifyGraphQLAPI", name, None, opts) self.amplify_api_name = amplify_api_name self.stack_name = amplify_api_name self.random_chars = RandomId.generate(8) self.amplify_api_build_dir = ( Path("amplify/backend/api").joinpath(amplify_api_name).joinpath("build") ) schema_path = self.amplify_api_build_dir.joinpath("schema.graphql") schema = schema_path.read_text() graphql_api = appsync.GraphQLApi( f"{self.stack_name}_graphql_api", authentication_type="AMAZON_COGNITO_USER_POOLS", user_pool_config={ "default_action": "ALLOW", "user_pool_id": user_pool.id, "app_id_client_regex": user_pool_client.id, }, schema=schema, ) for type_name in graphql_types: resources = self.generate_dynamo_data_source(graphql_api, type_name) exports_file = AmplifyExportsFile( f"{self.stack_name}_exports_file", source_directory=client_source_path, parameters={ "aws_project_region": config.region, "aws_cognito_region": config.region, "aws_user_pools_id": user_pool.id, "aws_user_pools_web_client_id": user_pool_client.id, "aws_appsync_graphqlEndpoint": graphql_api.uris["GRAPHQL"], "aws_appsync_region": config.region, "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS", }, ) self.set_outputs({"graphql_api_uri": graphql_api.uris["GRAPHQL"]})
addTenant(id: ID!, name: String!): Tenant! } type Tenant { id: ID! name: String } schema { query: Query mutation: Mutation } """ ## Create API accessible with a key api = appsync.GraphQLApi("key", authentication_type="API_KEY", schema=schema) api_key = appsync.ApiKey("key", api_id=api.id) ## Link a data source to the Dynamo DB Table data_source = appsync.DataSource("tenants-ds", name="TenantsDataSource", api_id=api.id, type="AMAZON_DYNAMODB", dynamodb_config={"table_name": table.name}, service_role_arn=role.arn) ## A resolver for the [getTenantById] query get_resolver = appsync.Resolver("get-resolver", api_id=api.id, data_source=data_source.name,
schema = schema_path.read_text() # Resources user_pool = cognito.UserPool("MyUserPool") user_pool_client = cognito.UserPoolClient("MyUserPoolClient", user_pool_id=user_pool.id) stack_name = amplify_api_name graphql_api = appsync.GraphQLApi( f"{stack_name}_graphql_api", authentication_type="AMAZON_COGNITO_USER_POOLS", user_pool_config={ "default_action": "ALLOW", "user_pool_id": user_pool.id, "app_id_client_regex": user_pool_client.id }, schema=schema) def generate_dynamo_data_source(type_name): """ Generates a DynamoDB data source for the given GraphQL type. This includes the Dynamo table, the AppSync data source, a data source role, and the resolvers. NOTE: This function generates Dynamo tables with a hash key called `id`, but no other keys. :param type_name The name of the GraphQL type. This is the identifier which appears after the `type` keyword in the schema.