Skip to main content

A python library to call GraphQL APIs without using hardcoded strings

Project description

Python package Codacy Badge

py-graphql-mapper

Code Generation Test Pyhon-GraphQL Mapping Test

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.

This document contains a quick overview of the functionalities, for more details and options you can read here:

The package does not use any third-party libraries, it relies only on python 3 (3.10+) standard libraries.

Usage in a nutshell

Installation

Available in PyPI, the following command will install the library:

pip install py-graphql-mapper

Generate python code from schema

To generate the code execute the following command:

pgmcodegen generate ./pathToOutputFolder -apiArgs ./<pathToArgsFile>/generatorArgs.json

This command requires a json file containing the parameters needed to get the GraphQL schema

image

A sample is available in the main folder 'cli_args.json'.

The following python files will be generated:

  • enums.py
  • scalars.py
  • gql_simple_types.py
  • gql_types.py
  • type_refs.py
  • queries.py
  • mutations.py

These links show code generated using the library Github GraphQL API, Rapid GraphQL API and GeoDBCities API

More command options are 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):

Instantiate it adding GraphQL arguments if needed:

from .output.gdbc.queries import currencies

my_currencies = currencies(last=7, before='MTE=')

or add them using the field args

my_currencies._args.last = 7
my_currencies._args.before = 'MTE='

Then call export_gql_source 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": my_currencies.export_gql_source },
                            headers={
                                    "content-type": "application/json",
                                        "X-RapidAPI-Key": '123402mmri02fni230iif32jr420',
                                        "X-RapidAPI-Host": "geodb-cities-graphql.p.rapidapi.com"
                                    }
                            )

More details on how to set a query 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 pygqlmap.network import GQLResponse

gqlResponse = GQLResponse(response)

gqlResponse.map_gqldata_to_obj(myCurrenciesQuery.type)

print('Result object: ' + str(gqlResponse.result_obj))

The mapped response from the GraphQL server will be available within gqlResponse object: _gqlResponse.result_obj_

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

py-graphql-mapper-1.1.2.tar.gz (37.8 kB view details)

Uploaded Source

Built Distribution

py_graphql_mapper-1.1.2-py3-none-any.whl (43.4 kB view details)

Uploaded Python 3

File details

Details for the file py-graphql-mapper-1.1.2.tar.gz.

File metadata

  • Download URL: py-graphql-mapper-1.1.2.tar.gz
  • Upload date:
  • Size: 37.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for py-graphql-mapper-1.1.2.tar.gz
Algorithm Hash digest
SHA256 808741870d6443db83ae6cdea94d196531286b029eb806abb434dbdabebd06a6
MD5 5497c1619f7cd09ed3b61b5781d3d800
BLAKE2b-256 d02a3f13fd714a55e22a950deb07b67204a54877480a78e689ebc77cd09989e9

See more details on using hashes here.

File details

Details for the file py_graphql_mapper-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for py_graphql_mapper-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3e83ce5935a54304408b73a8cfd5ef0ee6b288b0982f06fa6c20d02585a52114
MD5 c182d8f37afa1773e834397430a014bb
BLAKE2b-256 02b8ed4d294131376f256c6670da3fe7cb12c1a0e47d6687ecfd5c6375d55b37

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page