Skip to content

How to create AppSync resolver from the AWS JS V3 SDK programmatically

0

I have the following function that uses the AWS JS sdk v3 (npm package '@aws-sdk/client-appsync'):

async createCreateResolver({ apiId, dataSourceName, type, code }) {

        const params = {
            apiId,
            typeName: 'Mutation',
            fieldName: `create${type}`,
            dataSourceName,
            kind: 'UNIT',
            runtime: 'APPSYNC_JS',
            code
        }
        console.error(params)
        const command = new CreateResolverCommand(params)
        return await this.appSyncClient.send(command)
    }

I'm getting a Bad Request (400) error with no details other than 'BadRequestException: Request can't be parsed'. Any help would be appreciated.

asked 2 years ago298 views

1 Answer
0
Accepted Answer

To create an AppSync resolver using the AWS SDK for JavaScript v3 programmatically, you're on the right track with your function. However, there are a few things to consider that might be causing the Bad Request (400) error:

  1. Make sure all required parameters are provided. For a UNIT resolver, you need apiId, typeName, fieldName, and at least one of dataSourceName, requestMappingTemplate, or code.

  2. The 'code' parameter should contain the resolver code that includes both request and response functions. This is required when using the APPSYNC_JS runtime.

  3. Ensure the 'code' string is properly formatted and contains valid JavaScript for the APPSYNC_JS runtime.

  4. The 'typeName' should match a type in your GraphQL schema. In your case, you're using 'Mutation', which is correct if you're creating a mutation resolver.

  5. The 'fieldName' should match a field in your GraphQL schema. Your dynamic field name (create${type}) should correspond to an actual field in your schema.

  6. Verify that the 'dataSourceName' exists in your AppSync API.

  7. Double-check that the 'apiId' is correct and that you have the necessary permissions to create resolvers in that API.

Here's a slightly modified version of your function that might help:

async createCreateResolver({ apiId, dataSourceName, type, code }) {
const params = {
apiId,
typeName: 'Mutation',
fieldName: `create${type}`,
dataSourceName,
kind: 'UNIT',
runtime: {
name: 'APPSYNC_JS',
runtimeVersion: '1.0.0'
},
code
}
console.log('CreateResolver params:', JSON.stringify(params, null, 2));
const command = new CreateResolverCommand(params);
try {
return await this.appSyncClient.send(command);
} catch (error) {
console.error('Error creating resolver:', error);
throw error;
}
}

This version includes more detailed logging and error handling, which might help you identify the specific issue. If you're still encountering problems, you may want to check the AWS CloudWatch logs for more detailed error messages.

Remember to ensure that your 'code' parameter contains valid resolver code for both request and response handling in the APPSYNC_JS runtime format.

Sources
CreateResolver - AWS AppSync
AWS AppSync JavaScript resolver function reference for HTTP - AWS AppSync
Resolver - AWS AppSync

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.