Skip to content

Django App Cruddals

A base class for defining GraphQL schemas for a Django app using Cruddals.

This class provides methods to dynamically create GraphQL schemas for each model in the app based on provided settings.

This class is a wrapper around DjangoModelCruddals to generate GraphQL schema for each model in the app, avoiding the need to define a subclass for each model. Otherwise this approach yet allow customizing the behavior of the generated GraphQL schema for each model with the same attributes as DjangoModelCruddals but in a dictionary format.

Attributes:

Name Type Description
app_name str

Name of the Django app for which the schema will be generated.

schema

The generated GraphQL schema for the app.

Query

The combined Query object for the app.

Mutation

The combined Mutation object for the app.

meta

A dictionary with the name of the models as keys and the generated classes as values.

To use this class, define a subclass and include a Meta class with the required attributes.

Example Usage:

from graphene_django_cruddals import DjangoAppCruddals

class YourAppCruddals(DjangoAppCruddals):
    class Meta:
        app_name = "your_app"
from graphene_django_cruddals import DjangoAppCruddals

class YourAppCruddals(DjangoAppCruddals):
    class Meta:
        app_name = "your_app"
        models = ("YourModel1", "YourModel2")
        # exclude_models = ("YourModel1",)
        prefix = "New"
        suffix = "Suffix"
        cruddals_interfaces = (CustomCruddalsInterface,)
        functions = ("create", "read", "update", "delete", "deactivate", "activate", "list", "search")
        # exclude_functions = ("create",)
        settings_for_model = {
            "YourModel1": {
                "functions": ("create", "read", "update", "delete", "deactivate", "activate", "list", "search"),
                # "exclude_functions": ("create",),
                "prefix": "New",
                "suffix": "Suffix",
                "cruddals_interfaces": (CustomCruddalsInterfaceForYourModel1,),
                # "exclude_cruddals_interfaces": ("CustomCruddalsInterface",),
                "field_for_activate_deactivate": "is_enabled",
            },
            "YourModel2": {
                # ...
            },
        }

Meta Class Options:

Note

These options can also be seen below in the definition of __init_subclass_with_meta__ method.

*app_name (str):* Name of the Django app for which the schema will be generated.
*models (Tuple[str, ...], optional):* Names of the models to include in the generated GraphQL Schema. Defaults to None, in which case all models in the app will be included.
*exclude_models (Tuple[str, ...], optional):* Names of the models to exclude from the generated GraphQL Schema. Defaults to None.
*prefix (str, optional):* A prefix to be added to the generated GraphQL Types name. Defaults to "".
*suffix (str, optional):* A suffix to be added to the generated GraphQL Types name. Defaults to "".
*cruddals_interfaces (Tuple[Type[Any], ...], optional):* A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
*exclude_cruddals_interfaces (Tuple[str, ...], optional):* A tuple of interface names to be excluded if `DjangoProjectCruddals` is used. Defaults to ().
*functions (Tuple[FunctionType, ...], optional):* A tuple of functions ('create', 'read', 'update', 'delete', 'deactivate', 'activate', 'list', 'search') to be included in the generated GraphQL Schema. Defaults to None.
*exclude_functions (Tuple[FunctionType, ...], optional):* A tuple of functions to be excluded from the generated GraphQL Schema. Defaults to None.
*settings_for_model (Dict[str, Any], optional):* A dictionary with the name of the models as keys and a dictionary with the settings for each model as values for overriding the default settings of the generated GraphQL Schema. Defaults to {}.
Source code in graphene_django_cruddals/main.py
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
class DjangoAppCruddals(SubclassWithMeta):
    """

    A base class for defining GraphQL schemas for a **Django app** using Cruddals.

    This class provides methods to dynamically create GraphQL schemas for each model in the app based on provided settings.

    This class is a wrapper around `DjangoModelCruddals` to generate GraphQL schema for each model in the app, avoiding the need to define a subclass for each model.
    Otherwise this approach yet allow customizing the behavior of the generated GraphQL schema for each model with the same attributes as `DjangoModelCruddals` but in a dictionary format.

    Attributes:
        app_name (str): Name of the Django app for which the schema will be generated.
        schema: The generated GraphQL schema for the app.
        Query: The combined Query object for the app.
        Mutation: The combined Mutation object for the app.
        meta: A dictionary with the name of the models as keys and the generated classes as values.

    To use this class, define a subclass and include a `Meta` class with the required attributes.

    Example Usage:
    --------------

    === "Only Required Arguments"

        ```python
        from graphene_django_cruddals import DjangoAppCruddals

        class YourAppCruddals(DjangoAppCruddals):
            class Meta:
                app_name = "your_app"
        ```
    === "With Optional Arguments"

        ```python
        from graphene_django_cruddals import DjangoAppCruddals

        class YourAppCruddals(DjangoAppCruddals):
            class Meta:
                app_name = "your_app"
                models = ("YourModel1", "YourModel2")
                # exclude_models = ("YourModel1",)
                prefix = "New"
                suffix = "Suffix"
                cruddals_interfaces = (CustomCruddalsInterface,)
                functions = ("create", "read", "update", "delete", "deactivate", "activate", "list", "search")
                # exclude_functions = ("create",)
                settings_for_model = {
                    "YourModel1": {
                        "functions": ("create", "read", "update", "delete", "deactivate", "activate", "list", "search"),
                        # "exclude_functions": ("create",),
                        "prefix": "New",
                        "suffix": "Suffix",
                        "cruddals_interfaces": (CustomCruddalsInterfaceForYourModel1,),
                        # "exclude_cruddals_interfaces": ("CustomCruddalsInterface",),
                        "field_for_activate_deactivate": "is_enabled",
                    },
                    "YourModel2": {
                        # ...
                    },
                }
        ```

    Meta Class Options:
    -------------------

    Note:
        These options can also be seen below in the definition of `__init_subclass_with_meta__` method.

    ```markdown
    *app_name (str):* Name of the Django app for which the schema will be generated.
    *models (Tuple[str, ...], optional):* Names of the models to include in the generated GraphQL Schema. Defaults to None, in which case all models in the app will be included.
    *exclude_models (Tuple[str, ...], optional):* Names of the models to exclude from the generated GraphQL Schema. Defaults to None.
    *prefix (str, optional):* A prefix to be added to the generated GraphQL Types name. Defaults to "".
    *suffix (str, optional):* A suffix to be added to the generated GraphQL Types name. Defaults to "".
    *cruddals_interfaces (Tuple[Type[Any], ...], optional):* A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
    *exclude_cruddals_interfaces (Tuple[str, ...], optional):* A tuple of interface names to be excluded if `DjangoProjectCruddals` is used. Defaults to ().
    *functions (Tuple[FunctionType, ...], optional):* A tuple of functions ('create', 'read', 'update', 'delete', 'deactivate', 'activate', 'list', 'search') to be included in the generated GraphQL Schema. Defaults to None.
    *exclude_functions (Tuple[FunctionType, ...], optional):* A tuple of functions to be excluded from the generated GraphQL Schema. Defaults to None.
    *settings_for_model (Dict[str, Any], optional):* A dictionary with the name of the models as keys and a dictionary with the settings for each model as values for overriding the default settings of the generated GraphQL Schema. Defaults to {}.
    ```

    """

    Query = None
    Mutation = None
    schema = None

    meta = None

    @classmethod
    def __init_subclass_with_meta__(
        cls,
        app_name=None,
        models=None,
        exclude_models=None,
        prefix="",
        suffix="",
        cruddals_interfaces=(),
        exclude_cruddals_interfaces=(),
        functions=(),
        exclude_functions=(),
        settings_for_model=None,
        **kwargs,
    ):
        """
        Initialize the subclass with meta settings and dynamically create GraphQL schemas for the app.
        This method is called automatically when a subclass with a `Meta` class is defined. There is no need to call this method directly.

        Args:
            app_name (str): Name of the Django app for which the schema will be generated.
            models (tuple): Names of the models to include in the generated GraphQL Schema. Defaults to None, in which case all models in the app will be included.
            exclude_models (tuple): Names of the models to exclude from the generated GraphQL Schema. Defaults to None.
            prefix (str): A prefix to be added to the generated GraphQL Types name. Defaults to "".
            suffix (str): A suffix to be added to the generated GraphQL Types name. Defaults to "".
            cruddals_interfaces (tuple): A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
            exclude_cruddals_interfaces (tuple): A tuple of interface names to be excluded if `DjangoProjectCruddals` is used. Defaults to ().
            functions (tuple): Functions to include in the schemas can be "create", "read", "update", "delete", "deactivate", "activate", "list", "search" .
            exclude_functions (tuple): Functions to exclude from schema generation.
            settings_for_model (dict): A dictionary with the name of the models as keys and a dictionary with the settings for each model as values for overriding the default settings of the generated GraphQL Schema. Defaults to {}.
            **kwargs (dict) : Additional keyword arguments.

        Raises:
            AssertionError: If the `app_name` argument is not provided.

        """
        assert app_name, "app_name is required for DjangoAppCruddals"
        validate_list_func_cruddals(functions, exclude_functions)

        [setattr(cls, attr, None) for attr in ["Query", "Mutation", "schema", "meta"]]

        if settings_for_model is None:
            settings_for_model = {}

        cruddals_of_app = BuilderCruddalsApp(
            app_name=app_name,
            exclude_models=exclude_models,
            models=models,
            prefix=prefix,
            suffix=suffix,
            cruddals_interfaces=cruddals_interfaces,
            exclude_cruddals_interfaces=exclude_cruddals_interfaces,
            functions=functions,
            exclude_functions=exclude_functions,
            settings_for_model=settings_for_model,
        )
        cls.meta = cruddals_of_app

        cls.schema, cls.Query, cls.Mutation = get_schema_query_mutation(
            cruddals_of_app.queries, {}, cruddals_of_app.mutations, {}
        )

        super().__init_subclass_with_meta__(**kwargs)

__init_subclass_with_meta__ classmethod

__init_subclass_with_meta__(
    app_name=None,
    models=None,
    exclude_models=None,
    prefix="",
    suffix="",
    cruddals_interfaces=(),
    exclude_cruddals_interfaces=(),
    functions=(),
    exclude_functions=(),
    settings_for_model=None,
    **kwargs
)

Initialize the subclass with meta settings and dynamically create GraphQL schemas for the app. This method is called automatically when a subclass with a Meta class is defined. There is no need to call this method directly.

Parameters:

Name Type Description Default
app_name str

Name of the Django app for which the schema will be generated.

None
models tuple

Names of the models to include in the generated GraphQL Schema. Defaults to None, in which case all models in the app will be included.

None
exclude_models tuple

Names of the models to exclude from the generated GraphQL Schema. Defaults to None.

None
prefix str

A prefix to be added to the generated GraphQL Types name. Defaults to "".

''
suffix str

A suffix to be added to the generated GraphQL Types name. Defaults to "".

''
cruddals_interfaces tuple

A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().

()
exclude_cruddals_interfaces tuple

A tuple of interface names to be excluded if DjangoProjectCruddals is used. Defaults to ().

()
functions tuple

Functions to include in the schemas can be "create", "read", "update", "delete", "deactivate", "activate", "list", "search" .

()
exclude_functions tuple

Functions to exclude from schema generation.

()
settings_for_model dict

A dictionary with the name of the models as keys and a dictionary with the settings for each model as values for overriding the default settings of the generated GraphQL Schema. Defaults to {}.

None
**kwargs dict)

Additional keyword arguments.

{}

Raises:

Type Description
AssertionError

If the app_name argument is not provided.

Source code in graphene_django_cruddals/main.py
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
@classmethod
def __init_subclass_with_meta__(
    cls,
    app_name=None,
    models=None,
    exclude_models=None,
    prefix="",
    suffix="",
    cruddals_interfaces=(),
    exclude_cruddals_interfaces=(),
    functions=(),
    exclude_functions=(),
    settings_for_model=None,
    **kwargs,
):
    """
    Initialize the subclass with meta settings and dynamically create GraphQL schemas for the app.
    This method is called automatically when a subclass with a `Meta` class is defined. There is no need to call this method directly.

    Args:
        app_name (str): Name of the Django app for which the schema will be generated.
        models (tuple): Names of the models to include in the generated GraphQL Schema. Defaults to None, in which case all models in the app will be included.
        exclude_models (tuple): Names of the models to exclude from the generated GraphQL Schema. Defaults to None.
        prefix (str): A prefix to be added to the generated GraphQL Types name. Defaults to "".
        suffix (str): A suffix to be added to the generated GraphQL Types name. Defaults to "".
        cruddals_interfaces (tuple): A tuple of additional Cruddals interfaces to be implemented by the generated GraphQL Schema. Defaults to ().
        exclude_cruddals_interfaces (tuple): A tuple of interface names to be excluded if `DjangoProjectCruddals` is used. Defaults to ().
        functions (tuple): Functions to include in the schemas can be "create", "read", "update", "delete", "deactivate", "activate", "list", "search" .
        exclude_functions (tuple): Functions to exclude from schema generation.
        settings_for_model (dict): A dictionary with the name of the models as keys and a dictionary with the settings for each model as values for overriding the default settings of the generated GraphQL Schema. Defaults to {}.
        **kwargs (dict) : Additional keyword arguments.

    Raises:
        AssertionError: If the `app_name` argument is not provided.

    """
    assert app_name, "app_name is required for DjangoAppCruddals"
    validate_list_func_cruddals(functions, exclude_functions)

    [setattr(cls, attr, None) for attr in ["Query", "Mutation", "schema", "meta"]]

    if settings_for_model is None:
        settings_for_model = {}

    cruddals_of_app = BuilderCruddalsApp(
        app_name=app_name,
        exclude_models=exclude_models,
        models=models,
        prefix=prefix,
        suffix=suffix,
        cruddals_interfaces=cruddals_interfaces,
        exclude_cruddals_interfaces=exclude_cruddals_interfaces,
        functions=functions,
        exclude_functions=exclude_functions,
        settings_for_model=settings_for_model,
    )
    cls.meta = cruddals_of_app

    cls.schema, cls.Query, cls.Mutation = get_schema_query_mutation(
        cruddals_of_app.queries, {}, cruddals_of_app.mutations, {}
    )

    super().__init_subclass_with_meta__(**kwargs)