Last active
February 2, 2022 23:02
-
-
Save bencord0/70f9de572f0e284c94b7bcbf918dc0eb to your computer and use it in GitHub Desktop.
Accessing AppSync GraphQL APIs from python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! very helpful
To work with my appsync deployment I had to also pass in the session token from boto to AWS4Auth