Package: kirjava
kirjava is a Python GraphQL client.Example>>> import kirjava
>>> client = kirjava.Client("https://api.coolsite.com/")
>>> client.execute("""{ me { name email }}""")
{'data': {'me': {'name': 'Jon Snow', 'email': 'jon@winterfell.gov.ws'}}}Installingpipkirjava can be installed using pip:$ pip3 install kirjavaIf you get permission errors, try usingsudo:$ sudo pip3 install kirjavaOr alternatively, consider using a virtual environment.DevelopmentThe repository for kirjava, containing the most recent iteration, can be
foundhere. To clone the
kirjava repository directly from there, use:$ git clonegit://github.com/samirelanduk/kirjava.gitRequirementskirjava requiresrequests.Overviewkirjava is a lightweight Python GraphQL client.Making Queries with a ClientGraphQL services are interacted with using aClientobject:>>> import kirjava
>>> client = kirjava.Client("https://api.coolsite.com/")The client is associated with a particular URL upon creation.Queries are then made using theexecutemethod.>>> client.execute("{ me { name email }}")
{'data': {'me': {'name': 'Jon Snow', 'email': 'jon@winterfell.gov.ws'}}}If authentication tokens need to be added, they can be inserted into the
headers:>>> client.headers["Authorization"] = "dani123"Variables can be passed along with the query:>>> client.execute("{ me { name email }}", variables={"var1": 123})You can instruct the client to retry failed requests:>>> client.execute("{ me { name email }}", retries=3, retry_statuses=[500, 502, 503, 504])You can see all previous queries made by a client:>>> client.history
(({'string': { me { name email }}, 'variables': {'var1': 123}, {'data': {'me
': {'name': 'Jon Snow', 'email': 'jon@winterfell.gov.ws'}}}), ({'string': {
me { name email }}, 'variables': {}}, {'data': {'me': {'name': 'Jon Snow', '
email': 'jon@winterfell.gov.ws'}}}))Clients userequestssessions internally,
and you can access any cookies set by the server viaclient.session.cookies.Uploading FilesIf you want to upload files as part of your request, kirjava can do this. Just
add them as a variable:>>> mutation = "mutation sendFile($file: Upload) {sendFile(file: $file) { success }}"
>>> f = open("local_file.txt", "rb"):
>>> response = client.execute(mutation, variables={"file": f})
>>> f.close()kirjava does this by implementing theGraphQL multipart request specificationunder the hood, and using this if any of the variables supplied are Python file
objects.Note that the GraphQL server on the other end must be set up to process
multipart requests.Making Queries without a ClientAlternatively, if creating a dedicatedClientobject is somehow
beneath you, and you just want to fire off a quick request without any of that
overhead, there is a module levelexecutefunction:>>> kirjava.execute("https://api.coolsite.com/", "{ me { name email }}", headers={"Authorization": "dani123"}, variables={"var1": 123})ChangelogRelease 0.4.021 August 2023You can retry failed requests.Release 0.3.06 August 2020File upload array type now supported.Release 0.2.011 December 2020Implements GraphQL multipart request specification to allow file upload.Refactored kirjava.py into full package.Release 0.1.316 November 2020Provides access to requests cookie jar.Better handling of non-JSON responses.Release 0.1.21 April 2019Added module-level execute function.Release 0.1.130 March 2019Added tests.Clients now store history of their queries.Release 0.1.023 March 2019Created basic Client.