How do I create an AWS AppSync Resolver?
In this example, we will create an AWS AppSync Resolver using Pulumi. An AppSync Resolver is used to connect a GraphQL field to a data source. We will define a GraphQL API, a data source, and a resolver that links a specific field in a GraphQL type to the data source.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Define the GraphQL API
const graphqlApi = new aws.appsync.GraphQLApi("exampleApi", {
authenticationType: "API_KEY",
schema: `
type Query {
getMessage: String
}
schema {
query: Query
}
`,
});
// Define the data source
const dataSource = new aws.appsync.DataSource("exampleDataSource", {
apiId: graphqlApi.id,
type: "NONE",
});
// Define the resolver
const resolver = new aws.appsync.Resolver("exampleResolver", {
apiId: graphqlApi.id,
type: "Query",
field: "getMessage",
dataSource: dataSource.name,
requestTemplate: `
{
"version": "2017-02-28",
"payload": {}
}
`,
responseTemplate: `
$util.toJson("Hello, world!")
`,
});
// Export the GraphQL API URL
export const graphqlApiUrl = graphqlApi.uris["GRAPHQL"];
Key Points
- We created a GraphQL API using
aws.appsync.GraphQLApi
. - We defined a data source with
aws.appsync.DataSource
. - We created a resolver with
aws.appsync.Resolver
to link thegetMessage
field to the data source. - The resolver uses request and response templates to handle the GraphQL query.
Summary
In this example, we demonstrated how to create an AWS AppSync Resolver using Pulumi. We defined a GraphQL API, created a data source, and linked a field in the GraphQL schema to the data source using a resolver. This setup allows us to handle GraphQL queries and return responses.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.