Skip to content

A python library for generating GraphQL queries and mutations using plain python objects, no hardcoded strings.Any questions or issues, please report here https://github.com/dapalex/py-graphql-mapper/issues

License

Notifications You must be signed in to change notification settings

dapalex/py-graphql-mapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d5d63f2 · Aug 14, 2023
Aug 11, 2023
Aug 10, 2023
Jan 16, 2023
Aug 14, 2023
Aug 14, 2023
Oct 2, 2022
Jul 2, 2023
Oct 2, 2022
Jan 8, 2023
Aug 12, 2023
Aug 11, 2023
Jan 3, 2023
Aug 11, 2023
Jul 1, 2023
Jan 7, 2023
Aug 11, 2023

Repository files navigation

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

"Buy Me A Coffee"

About

A python library for generating GraphQL queries and mutations using plain python objects, no hardcoded strings.Any questions or issues, please report here https://github.com/dapalex/py-graphql-mapper/issues

Topics

Resources

License

Stars

Watchers

Forks

Languages