Metadata-Version: 2.1
Name: graphene-generator
Version: 0.1.2
Summary: A Graphene-Django app to generate GraphQL mutations and Queries.
Home-page: https://github.com/alainburindi/graphene_generator
Author: Alain Burindi
Author-email: alainburindi62@gmail.com
License: MIT
Platform: UNKNOWN
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 3.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Topic :: Software Development :: Libraries

Graphene-Generator
------------------

A
`Graphene-Django <https://github.com/graphql-python/graphene-django>`__
(GraphQL) queries and mutations generator

You can:
~~~~~~~~

-  Generate queries and mutations based on the specified model(s)
-  Require authentication for some and/or all generated
   queries/mutations

Tech
~~~~

Graphene-Generator uses a number of open source projects to work
properly:

-  `Django <https://github.com/django/django>`__ - a Python-based web
   framework,
-  `Graphene-Django <https://github.com/graphql-python/graphene-django>`__
   - A Django integration for Graphene.

If you are not familiar with the above technologies, please refer to
their respective documentation.

And of course Graphene-generator itself is open source with a `public
repository <https://github.com/alainburindi/graphene_generator>`__ on
GitHub.

Quickstart
~~~~~~~~~~

For installing graphene, just run this command in your shell:

.. code:: bash

   pip install "graphene-generator"

Settings
^^^^^^^^

We need to specify the model(s) name to be used and their respective
path(s)

.. code:: python

       GRAPHENE_GENERATOR_MODELS = [
           {
               'name': 'ingredient',
               'path': 'path.to.the.model',
           }
       ]

Note that ``GRAPHENE_GENERATOR_MODELS`` is an array to support many
models at once.

Authentication
^^^^^^^^^^^^^^

If we want to require the authentication, we need to specify that in our
settings under the ``require_auth`` dictionary for each model

.. code:: python

       GRAPHENE_GENERATOR_MODELS = [
           {
               # ...
               'require_auth': {
                   'queries': ["all", "single"],
                   'mutations': ["create", 'update', 'delete']
               }
           }
       ]

To make the difference between Mutations and Queries the
``require_auth`` contains ``queries`` and ``mutations`` as different
keys.

Below are the different values and their meaning:

Queries
'''''''

========== =====================================================
Key word   Meaning
========== =====================================================
``all``    The get all query (usually the ``model['name'] + s``)
``single`` The get one query (usually the ``model['name']``)
========== =====================================================

Mutations
'''''''''

========== ===================
Key word   Meaning
========== ===================
``create`` The create mutation
``update`` The update mutation
``delete`` The delete mutation
========== ===================

Schema
^^^^^^

We need to import the ``QueriesHolder`` and/or ``MutationsHolder``
classes into our schema used by graphene and you should be able to see
the generated CRUD operations into you schema.

Examples
--------

Here is a simple Django model:

.. code:: python

   from django.db import models

   class Ingredient(models.Model):
       name = models.CharField(max_length=100)
       notes = models.TextField()

Based on the above model ou settings would look like:

.. code:: python

   GRAPHENE_GENERATOR_MODELS = [
       {
           'name': 'ingredient',
           'path': 'ingredients.models.Ingredient',
           'require_auth': {
               'queries': ["all", "single"],
               'mutations': ["create", 'update', 'delete']
           }
       }
   ]

Here is a graphene schema sample which use the generated requests:

.. code:: python

   import graphene

   from graphene_generator.holder import QueriesHolder, MutationsHolder


   class Query(QueriesHolder, graphene.ObjectType):
       pass


   class Mutation(MutationsHolder, graphene.ObjectType):
       pass


   schema = graphene.Schema(query=Query, mutation=MutationsHolder)

Then you can query the schema:

.. code:: python

   query = '''
       query {
         ingredients {
           name,
           notes
         }
       }
   '''
   result = schema.execute(query)

Todos
~~~~~

-  Write Tests
-  Handle model’s relations properly
-  Use corresponding graphene scalar type for each field(currently using
   string for all fields)
-  Handle pagination

License
-------

MIT


