BSD
BSD

Reputation: 71

Wrapping graphQL(appsync) with REST API using Amazon api gateway

I have a graphQL server deployed with aws appsync. The thing is that our customers prefer a standard REST API. I'm looking for the simplest way to wrap graphQL query with REST API.

I'm considering using Amazon api gateway to make a REST endpoint, and integrate lambda behind the api gateway. In that way I can let lambda functions to send a fixed graphQL query/mutations and modify the response.

However as you can see from below image, I found AWS Service integration option in Amazon API gateway. I'm wondering whether I can integrate appsync to api gateway directly without using lambda. I searched it from aws documents but couldn't find any related information.

Amazon api gateway setup capture:

Amazon api gateway setup capture

  1. Is it possible to wrap graphQL API with REST API by integrating appsync to api gateway without using lambda? Just like what I found from the captured image?
  2. If yes, is there any examples or tutorials?
  3. If not, should I just integrate lambda? Is there any better ideas or tips?

Upvotes: 5

Views: 4752

Answers (3)

vini
vini

Reputation: 23

Adding on to @BSD's solution:

  • IAM role:

    • should grant Trust relationship to ApiGateway
      {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Effect": "Allow",
            "Principal": {
              "Service": "apigateway.amazonaws.com"
            },
            "Action": "sts:AssumeRole",
            "Condition": {}
          }
        ]
      }
      
    • should have policy to allow to access AppSync endpoint
      {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Action": [
                      "appsync:GraphQL"
                  ],
                  "Resource": [
                      "arn:aws:appsync:us-east-1:{AWS-Account-Number}:apis/{AppSync-API-ID}/*"
                  ],
                  "Effect": "Allow"
              }
          ]
      }
      
  • The format for sending query/mutation through APIGW: Let's say your AppSync query is:

query MyQuery {
  foo(request: {bar: "abc123", baz: "xyz"}) {
    a
    b
    c
  }
}

This query turns into the following JSON:

{
  "query": "query MyQuery {foo(request: {bar: \"abc123\", baz: \"xyz\"}) {a b c} }"
}

Upvotes: 1

nerzie
nerzie

Reputation: 31

In case someone using OpenAPI spec for defining APIGW, use the following:

/graphql:
  post:
    x-amazon-apigateway-integration:
      type: "AWS"
      httpMethod: "POST"
      uri: arn:aws:apigateway:<APIGW_REGION>:<APPSYNC_URL_ID>.appsync-api:path/graphql
      credentials: <INVOCATION_ROLE_ARN>

https://docs.aws.amazon.com/general/latest/gr/appsync.html#appsync_region_data_plane https://docs.aws.amazon.com/apigateway/api-reference/resource/integration/#type

Upvotes: 2

BSD
BSD

Reputation: 71

I found out how to integrate appsync to API gateway. You can do it with AWS Service integration by setting it as AppSync Data Plane.

In the method setup page:

  1. Integration type = AWS Service

  2. AWS Service = AppSync Data Plane

  3. AWS Subdomain = get it from your appsync endpoint (For example: https://YOUR_AWS_Subdomain.appsync-api.your-region.amazonaws.com/graphql)

  4. HTTP method = POST

  5. Action Type = Use path override

  6. Path override(optional) = graphql

It worked well for me.

Upvotes: 2

Related Questions