Use existing AWS resources
Re-use existing AppSync GraphQL API
As an alternative to automatic configuration, you can manually enter AWS AppSync configuration parameters in your app. Authentication type options are API_KEY
, AWS_IAM
, AMAZON_COGNITO_USER_POOLS
and OPENID_CONNECT
.
Using API_KEY
const myAppConfig = { // ... aws_appsync_graphqlEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql', aws_appsync_region: 'us-east-1', aws_appsync_authenticationType: 'API_KEY', aws_appsync_apiKey: 'da2-xxxxxxxxxxxxxxxxxxxxxxxxxx' // ...};
Amplify.configure(myAppConfig);
Using AWS_IAM
const myAppConfig = { // ... aws_appsync_graphqlEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql', aws_appsync_region: 'us-east-1', aws_appsync_authenticationType: 'AWS_IAM' // ...};
Amplify.configure(myAppConfig);
Using AMAZON_COGNITO_USER_POOLS
const myAppConfig = { // ... aws_appsync_graphqlEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql', aws_appsync_region: 'us-east-1', aws_appsync_authenticationType: 'AMAZON_COGNITO_USER_POOLS' // You have configured Auth with Amazon Cognito User Pool ID and Web Client Id // ...};
Amplify.configure(myAppConfig);
Using OPENID_CONNECT
const myAppConfig = { // ... aws_appsync_graphqlEndpoint: 'https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql', aws_appsync_region: 'us-east-1', aws_appsync_authenticationType: 'OPENID_CONNECT' // Before calling API.graphql(...) is required to do Auth.federatedSignIn(...) check authentication guide for details. // ...};
Amplify.configure(myAppConfig);
Using with an AppSync custom domain name
Custom domain names can have any format, but must end with /graphql
(see https://graphql.org/learn/serving-over-http/#uris-routes).
const myAppConfig = { // ... aws_appsync_graphqlEndpoint: 'https://api.yourdomain.com/graphql', aws_appsync_region: 'us-east-1', aws_appsync_authenticationType: 'API_KEY' // All auth modes are supported // ...};
Amplify.configure(myAppConfig);
Using a non-AppSync GraphQL Server
To access a non-AppSync GraphQL API with your app, you need to configure the endpoint URL in your app’s configuration. Add the following line to your setup:
import { Amplify, API } from 'aws-amplify';import awsconfig from './aws-exports';
// Considering you have an existing aws-exports.js configuration fileAmplify.configure(awsconfig);
// Configure a custom GraphQL endpointAmplify.configure({ API: { graphql_endpoint: 'https:/www.example.com/my-graphql-endpoint' }});
Set Custom Request Headers for non-AppSync GraphQL APIs
When working with a non-AppSync GraphQL endpoint, you may need to set request headers for authorization purposes. This is done by passing a graphql_headers
function into the configuration:
Amplify.configure({ API: { graphql_headers: async () => ({ 'My-Custom-Header': 'my value' }) }});
Signing Request with IAM
AWS Amplify provides the ability to sign requests automatically with AWS Identity Access Management (IAM) for GraphQL requests that are processed through AWS API Gateway. Add the graphql_endpoint_iam_region
parameter to your GraphQL configuration statement to enable signing:
Amplify.configure({ API: { graphql_endpoint: 'https://www.example.com/my-graphql-endpoint', graphql_endpoint_iam_region: 'my_graphql_apigateway_region' }});