Load resolvers
Up until now, we have not interacted with AppSync Butler. We have
- Defined a GraphQL schema.
- Setup the resolvers.
- Instantialized the AppSync API construct.
The AppSync construct is not yet aware of the on-disk resolvers.
AppSync Butler handles this process seamlessly. To load on-disk
resolvers and functions, use Loader
or
SstLoader
for CDK and SST applications respectively
- Serverless Stack Toolkit
- AWS Cloud Development Kit
stacks/MyStack.ts
import { StackContext, AppSyncApi } from "@serverless-stack/resources";
import { SstLoader as Loader } from '@appsync-butler/sst';
export function MyStack({ stack }: StackContext) {
const api = new AppSyncApi(stack, "api", {
schema: "graphql/index.graphql"
});
const loader = new Loader(stack, { api });
loader.load();
stack.addOutputs({
GraphQlApiEndpoint: api.url,
});
}
lib/app-stack.ts
import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { GraphqlApi, Schema } from '@aws-cdk/aws-appsync-alpha';
import { Loader } from '@appsync-butler/core';
export class AppStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const api = new GraphqlApi(this, "api", {
name: "time-pingpong-api",
schema: Schema.fromAsset("graphql/index.graphql")
});
const loader = new Loader(this, { api });
loader.load();
}
}
tip
Instead of explicitly associating resolvers with data sources in request mapping templates, you can specify a default data source when creating the loader.
const loader = new Loader(this, { api, defaultUnitResolverDataSource: 'none' });
For other options that you may pass, check CDK:LoaderOptions or SST:LoaderOptions.