Generate aws-cdk AppSync
-
install peer dependencies:
$ yarn add -D graphql @graphql-codegen/cli
-
install graphql-codegen-aws-cdk-appsync-resolver
$ yarn add -D graphql-codegen-aws-cdk-appsync-resolver
note: versions
aws-cdk graphql-codegen-aws-cdk-appsync-resolver <=1.59 <=0.3.2 >=1.60 >=0.4.0 -
add codegen.yml:
schema: - "./schema.graphql" generates: ./AppSyncResolver.ts: plugins: - "graphql-codegen-aws-cdk-appsync-resolver"
-
Your Graphql Schema
type Item { id: ID! name: String } type PaginatedItem { items: [Item!]! nextToken: String } type Query { all(limit: Int, nextToken: String): PaginatedItem! getOne(id: ID!): Item } type Mutation { save(name: String!): Item delete(id: ID!): Item } type Schema { query: Query mutation: Mutation }
-
generate AppSyncResolver Templates
$ yarn graphql-codegen
generated AppSyncResolver.ts
import { CfnResolver } from "@aws-cdk/aws-appsync"; import { Construct } from "@aws-cdk/core"; ... export const createQueryAllResolver = createResolver("Query", "all"); export const createQueryGetOneResolver = createResolver("Query", "getOne"); export const createMutationSaveResolver = createResolver("Mutation", "save"); export const createMutationDeleteResolver = createResolver("Mutation", "delete");
-
aws-cdk Usage Sample
// Your aws-cdk code import { GraphQLApi } from "@aws-cdk/aws-appsync"; import { Construct, Stack, StackProps } from "@aws-cdk/core"; import { createQueryGetUserResolver, createQueryListUsersResolver, } from "./your/path/to/generated/AppSyncResolver"; class TestStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); const api = new GraphQLApi( ... ); // crate GetOne Query resolver template createQueryGetOneResolver( this, { api, ... } ); // crate ListUsers Query resolver template createQueryListUsersResolver( this, name => `Dev${name}` // name function { api, ... } ); } }
synthesized CloudFormation Template
Resources: ItemsApi: Type: AWS::AppSync::GraphQLApi ... AppSyncResolverQueryGetUser: Type: AWS::AppSync::Resolver ... RequestMappingTemplate: >- { "version" : "2017-02-28", "operation": "Invoke", "type": QueryGetUser, ... } ResponseMappingTemplate: ... DevAppSyncResolverQueryListUsers: Type: AWS::AppSync::Resolver ... RequestMappingTemplate: >- { "version" : "2017-02-28", "operation": "Invoke", "type": QueryListUsers, ... } ResponseMappingTemplate: ... ...