Pavan Kumar
Pavan Kumar

Reputation: 145

Authenticating Graphql using cognito user pool

I need help in connecting to Graphql(AWS AppSync) API using Amazon Cognito pool using Javascript/Nodejs.

I am able to connect to Graphql using API key (API_KEY) as the authentication mechanism. But I need to change the Authentication mechanism to using Cognito pool (AMAZON_COGNITO_USER_POOLS).

I know that we need to collect the JWT token and connect to Graphql using the token. But I am not sure what changes needs to be done to the below code.

I went though the documentation and could not find any reference to the code implementation for Cognito authentication for Graphql. I have changed the 'aws_appsync_authenticationType' to 'AMAZON_COGNITO_USER_POOLS'. I am not sure about the parameter name to pass JWT token (similar to 'aws_appsync_authenticationType') and the implementation. Any suggestion is appreciated.

const { Amplify, API, graphqlOperation } = require('aws-amplify')

const awsConfig = {
    aws_project_region: 'us-east-1',
    aws_appsync_graphqlEndpoint: 'abc.com/graphql',
    aws_appsync_region: 'us-east-1',
    aws_appsync_authenticationType: 'API_KEY',
    aws_appsync_apiKey: '12345678',
    aws_cognito_identity_pool_id: 'abc123',
    aws_cognito_region: 'us-east-1',
    aws_user_pools_id: 'xyzpqr',
    aws_user_pools_web_client_id: '1a2b3c4d5e'
}

Amplify.configure(awsConfig)

API.graphql(graphqlOperation(query, parameter)).then(response => {
    console.log('response is ' + response)
}).catch(err => {
    console.log('erorr is ' + JSON.stringify(err))
})

Upvotes: 0

Views: 693

Answers (1)

razorRun
razorRun

Reputation: 110

Have a look at the script in here and replace value https://github.com/razorRun/appsync-cognito-apollo-amplify-test

Upvotes: 0

Related Questions