Skip to content

Instantly share code, notes, and snippets.

@iki
Last active May 13, 2021 17:41
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iki/d65eb46929c7c8cc306d911b5ae4384a to your computer and use it in GitHub Desktop.
Save iki/d65eb46929c7c8cc306d911b5ae4384a to your computer and use it in GitHub Desktop.
Example of using GraphQL API from Micropython with ported Prisma python-graphql-client
# See https://help.github.com/ignore-files/ for more about ignoring files.
# logs
*.log*
# tools settings and other hidden directories
/.*/

Example of using GraphQL API from Micropython with ported Prisma python-graphql-client

Running the example

# Build image
docker-compose build

# Run example
docker-compose run micropython

# Optionally run the container and open shell to play with micropython and graphql further
docker-compose run micropython bash
version: "3"
services:
micropython:
image: micropython:graphql
container_name: micropython:graphql
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/root/graphql
# Micropython with Prisma GraphQL client
# - https://micropython.org/
# - https://github.com/prismagraphql/python-graphql-client
# Install micropython
#
# Fedora has micropython package ready. Ubuntu/Debian don't. Arch only in AUR.
# - https://apps.fedoraproject.org/packages/micropython
# - https://aur.archlinux.org/packages/micropython/
#
# Alpine has micropython in edge/testing, but misses built-in ussl module needed for upip.
# - https://patchwork.alpinelinux.org/patch/3779/
# FROM alpine:edge
# RUN apk add micropython -X http://dl-cdn.alpinelinux.org/alpine/edge/testing
#
# There was a PyPI change which broke upip and the upip fix is not part of the micropython package yet.
# Temporary fix is to use micropip instead of upip.
# - https://github.com/micropython/micropython-lib/issues/274
FROM fedora:latest
RUN dnf -y install micropython
RUN curl -sL https://github.com/peterhinch/micropython-samples/raw/master/micropip/micropip.py -o /usr/bin/micropip
RUN chmod +x /usr/bin/micropip
# Install dependencies
WORKDIR /root/.micropython/lib
RUN micropip install micropython-urequests
# RUN micropython -m upip install micropython-urequests
# Run example
WORKDIR /root/graphql
CMD micropython example.py
from graphqlclient import GraphQLClient
def main():
client = GraphQLClient('https://api.graphloc.com/graphql')
result = client.execute('''
{
getLocation(ip: "8.8.8.8") {
country {
names {
en
}
geoname_id
iso_code
}
location {
latitude
longitude
}
}
}
''')
print(result)
if __name__ == '__main__':
main()
# Ported python-graphql-client to use micropython-urequests.
# - https://github.com/prismagraphql/python-graphql-client
# - https://github.com/micropython/micropython-lib/tree/master/urequests
from urequests import post
def shrink_query(query):
query = query.replace(r'\n', ' ')
query = query.replace(r'\t', ' ')
while query != query.replace(' ', ' '):
query = query.replace(' ', ' ')
return query
class GraphQLClient:
def __init__(self, endpoint, useGet=False):
self.endpoint = endpoint
self.useGet = useGet
self.token = None
def execute(self, query, variables=None):
return self._send(query, variables)
def inject_token(self, token):
self.token = token
def _send(self, query, variables):
query = shrink_query(query)
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
if self.token is not None:
headers['Authorization'] = '{}'.format(self.token)
response = post(
self.endpoint,
headers=headers,
json=dict(query=query, variables=variables))
return response.content.decode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment