A python library to call GraphQL APIs without using hardcoded strings
Project description
py-graphql-mapper
A python library to interact with GraphQL APIs with no need of hardcoded strings.
Introduction
This library acts as a mapper between python and GraphQL languages for GraphQL clients, allowing a code-first approach when calling a GraphQL API server. It translates GraphQL entities into python objects and viceversa in order to avoid working with massive "copy&paste"s.
The package does not use any third-party libraries, it relies only on python 3 (3.10+) standard libraries.
Usage in a nutshell
Installation
The following command will install the library:
python setup.py install
Generate python code from schema
To generate the code execute the following command:
pygqlcodegen generate ./pathToOutputFolder -apiArgs ./pathToArgsFile/generatorArgs.json
This command requires a json file containing the parameters needed to get the GraphQL schema
In the output folder there will be the following python files:
- enums.py
- scalars.py
- gqlSimpleTypes.py
- gqlTypes.py
- circularRefs.py
- queries.py
- mutations.py
The following links show code generated using the library Github GraphQL API, Rapid GraphQL API and GeoDBCities API
More command options available here
Execution of a query
Choose the query class you want to use from the generated file queries.py (or a mutation from mutations.py):
from .output.GeoDBCities.queries import currencies
myCurrenciesQuery = currencies()
populate the necessary information to execute the query:
myCurrenciesQuery._args.last = 7
myCurrenciesQuery._args.before = 'MTE='
Call exportGqlSource property to pass the payload to the HTTP request:
(example using requests library)
import requests
response = requests.request('POST', url='https://geodb-cities-graphql.p.rapidapi.com/',
json= { "query": myCurrenciesQuery.exportGqlSource },
headers={
"content-type": "application/json",
"X-RapidAPI-Key": '123402mmri02fni230iif32jr420',
"X-RapidAPI-Host": "geodb-cities-graphql.p.rapidapi.com"
}
)
More details here
Retrieval of a response
Obtained the response from the GraphQL API the following code will map the received json payload into the python object
from pgqlmCore.network import GQLResponse
gqlResponse = GQLResponse(response)
gqlResponse.mapGQLDataToObj(myCurrenciesQuery.type)
print('resultObject: ' + str(gqlResponse.resultObject))
The mapped response from the GraphQL server will be available within gqlResponse object: gqlResponse.resultObject
More details here
A suite of use cases here
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for py_graphql_mapper-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a3429ea863724d0c2b749b311a607c9eb592395df2b28d71dfe71569a4bbb9e |
|
MD5 | 7eb715b6bea40665da94eeb5f1d20186 |
|
BLAKE2b-256 | a6e5116b1aa6c474b0ac27c7cf59a5630f2253b338755d7fa513a68e356f255e |