Skip to content

Instantly share code, notes, and snippets.

@bencord0
Last active February 2, 2022 23:02
Show Gist options
  • Save bencord0/70f9de572f0e284c94b7bcbf918dc0eb to your computer and use it in GitHub Desktop.
Save bencord0/70f9de572f0e284c94b7bcbf918dc0eb to your computer and use it in GitHub Desktop.
Accessing AppSync GraphQL APIs from python
import json
import requests
from boto3.session import Session as AWSSession
from gql_py import Gql
from requests import Session
from requests_aws4auth import AWS4Auth
aws = AWSSession()
session = Session()
# Discover boto3 credentials, use them to manually sign appsync requests
credentials = aws.get_credentials().get_frozen_credentials()
# Create a `requests` compatible auth object
session.auth = AWS4Auth(
credentials.access_key,
credentials.secret_key,
aws.region_name,
'appsync',
session_token=credentials.token,
)
session.headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
# Use the lovely gql library by @phalt
API_URL = 'https://xxxxxxxxxxxxxx.appsync-api.eu-west-1.amazonaws.com/graphql'
api = Gql(api=API_URL, session=session)
list_entries = '''
query {
listEntries(limit: 10) {
items {
slug
}
}
}
'''
def main():
entries = api.send(query=list_entries)
print(entries.data)
if __name__ == '__main__':
main()
@bencord0
Copy link
Author

Thanks @mfogel, I've updated the gist with that feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment