How to update DB with GraphQL

Two options to update:

  • From AWS Console
  • Update with AWS SDK

From AWS Console

  • AWS Appsync>Queries
  • Input the following query and “RUN”
# query to update MV table:

mutation MyMutation($input: UpdateTableInput = {id: "mie-temp-mv", tags: [{name: "mie2"}], _version:5}) {
  updateTable(input: $input) {
    id
    tags {
      name
    }
    _version
  }
}

# _version is necessary. specify the given version

The query above will add
	tags: [{name: "mie2"}]
to the existing datebase.

Update with AWS SDK

import pai_gql
from boto3 import Session as AWSSession
from requests_aws4auth import AWS4Auth
from gql import gql
from gql.client import Client
from gql.transport.requests import RequestsHTTPTransport

query = """mutation MyMutation($input: UpdateTableInput = {id: "mie-temp-mv", tags: [{name: "mie2", _version:6}) {
            updateTable(input: $input) {
                id
                aiTags {
                name
                }
                _version
            }
            }"""

headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
}

aws = AWSSession()
credentials = aws.get_credentials().get_frozen_credentials()

auth = AWS4Auth(
    credentials.access_key,
    credentials.secret_key,
    aws.region_name,
    'appsync',
    session_token=credentials.token,
)
url = "your appsync url"
transport = RequestsHTTPTransport(url=url,
                                  headers=headers,
                                  auth=auth)
                                    
client = Client(transport=transport,
                fetch_schema_from_transport=True)

resp = client.execute(gql(query))
print(resp)

Reference:

https://dgraph.io/docs/graphql/mutations/deep/