如何借助现有REST API端点URL创建AWS AppSync API?
Convert a REST API to AWS AppSync API: Step-by-Step Guide
Hey there! Since you're new to AWS AppSync, let's walk through exactly how to turn your existing REST API into an AppSync API step by step. I'll keep it straightforward so you can follow along easily.
Step 1: Create an AWS AppSync API Instance
- Log into the AWS Management Console and navigate to the AWS AppSync service.
- Click Create API, then select the "Custom schema (build from scratch)" option. Give your API a name (something like
MyRESTtoAppSyncAPI) and hit Create.
Step 2: Add a REST Data Source
- From the left-hand menu of your AppSync API console, select Data sources.
- Click Create data source and fill in the details:
- Data source name: Pick a descriptive name (e.g.,
MyRESTBackend) - Data source type: Choose HTTP
- Endpoint: Paste your existing REST API's URL here
- Authorization mode: Select the option that matches your REST API's security setup. If it's a public endpoint, go with "None". For API keys, IAM, or OAuth, pick the corresponding option and fill in the required credentials.
- Data source name: Pick a descriptive name (e.g.,
- Click Create to save the data source.
Step 3: Define Your GraphQL Schema
- Head to the Schema tab in the left menu. Here, you'll translate your REST API's JSON response structure into GraphQL types.
- For example, if your REST API returns user data like this:
Your GraphQL schema would look like this:{ "id": "123", "fullName": "Jane Smith", "userEmail": "jane@example.com" }
Adjust the types and queries to match your actual REST API response.type User { id: ID! fullName: String! userEmail: String! } type Query { getCurrentUser: User }
Step 4: Set Up Resolvers
- Still in the Schema tab, find the query you defined (like
getCurrentUser) and click the Attach resolver button next to it. - Select the REST data source you created earlier from the dropdown.
- Now configure the mapping templates:
- Request Mapping Template: This tells AppSync how to send a request to your REST API. For a simple GET request, use this:
If you need a POST request or need to pass parameters, modify the{ "version": "2018-05-29", "method": "GET", "resourcePath": "/", # Replace with your REST API's specific path if needed (e.g., "/api/user") "params": { "headers": $utils.http.copyHeaders($ctx.request.headers) } }methodand add abodyfield inparams. - Response Mapping Template: This converts the REST API's JSON response into the GraphQL format you defined. Use this basic template:
$utils.toJson($ctx.result.body)
- Request Mapping Template: This tells AppSync how to send a request to your REST API. For a simple GET request, use this:
- Click Save resolver to finish setup.
Step 5: Test Your AppSync API
- Go to the Query tab in the left menu.
- In the GraphQL editor, enter a query that matches your schema. Using the example above:
query { getCurrentUser { id fullName userEmail } } - Click the Run button. You should see your REST API's data returned in the clean GraphQL format you defined!
Quick Extra Tips
- If your REST API uses query parameters, add them to the
params.querysection in the request template. - For mutation operations (like creating/updating data), define a
Mutationtype in your schema and set up a resolver with the appropriate HTTP method (POST/PUT/DELETE). - If you run into authorization issues, double-check the data source's authorization mode matches what your REST API expects.
内容的提问来源于stack exchange,提问作者Er Mitul Parmar
相关产品推荐
相关产品推荐

