MGE-GraphQL

Introduction
MGE-GraphQL is a Python library for building GraphQL mutations fast and easily.
- Data Validations: A similar data validation workflow as Django.
- Errors: Support for throwing errors
- Permissions: Support for user permissions
Installation
For instaling MGE-GraphQL, just run this command in your shell
pip install "mge-graphql"
Examples
Here is one example for you to get started:
Create error_codes.py and define some errors
from enum import Enum
from mge_graphql.utils.error_codes import (
MGE_ERROR_CODE_ENUMS,
generate_error_codes
)
class AccountErrorCode(Enum):
# Here you define your Error Codes
INVALID_PASSWORD = "invalid_password"
# Register error codes
MGE_ERROR_CODE_ENUMS.append(AccountErrorCode)
generate_error_codes()
Create enums.py to define your Graphene Error Enums
import graphene
import error_codes as account_error_codes
# Create Graphene Enum
AccountErrorCode = graphene.Enum.from_enum(account_error_codes.AccountErrorCode)
Create types.py to create your custom error graphql object type
from mge_graphql.types.common import Error
from enums import AccountErrorCode
class AccountError(Error):
# Custom fields
# Support for error_code
code = AccountErrorCode(description="The error code.", required=True)
Create mutations.py to create your first mutation
from mge_graphql.mutations.base import BaseMutation
from mge_graphql.exceptions import ValidationError
from types import AccountErrorCode
import graphene
class AccountRegister(BaseMutation):
# YOUR GRAPHENE FIELDS
username = graphene.String(required=True)
password = graphene.String(required=True)
class Arguments:
username = graphene.String(required=True)
password = graphene.String(required=True)
class Meta:
description = "Register a new account."
# Set our custom AccountError class
error_type_class = AccountError
@classmethod
def clean_password(cls, password, errors):
if len(password) < 6:
errors["password"].append(
ValidationError(
{
"password": ValidationError(
"Password cannot be less than 6 characters.",
code=AccountErrorCode.INVALID_PASSWORD
)
}
)
)
return password
@classmethod
def clean(cls, **data):
errors = defaultdict(list)
cls.clean_password(data["password"], errors)
if errors:
raise ValidationError(errors)
return data
@classmethod
def check_permissions(cls, context):
# Permission Checks.
# If False, then it will raise an Permission Denied Error
return True
@classmethod
def perform_mutation(cls, _root, info, **data):
cleaned_data = cls.clean(**data)
cleaned_username = cleaned_data.get("username")
cleaned_password = cleaned_data.get("password")
# User Save // Any Mutation Logic
return AccountRegister(
username=cleaned_username,
password=cleaned_password
)
Create schema.py and register your mutation:
from mutations import AccountRegister
import graphene
class Mutation(graphene.ObjectType):
account_register = AccountRegister.Field()
schema = graphene.Schema(mutation=Mutation)
And.. we are done! Let's try our mutation
invalid input:
mutation {
accountRegister(username: "test", password: "234") {
username
password
errors {
field
message
code
}
}
}
{
"data": {
"accountRegister": {
"username": null,
"password": null,
"errors": [
{
"field": "password",
"message": "Password cannot be less than 6 characters.",
"code": "INVALID_PASSWORD"
}
]
}
}
}
valid input:
mutation {
accountRegister(username: "test", password: "123456") {
username
password
errors {
field
message
code
}
}
}
{
"data": {
"accountRegister": {
"username": "test",
"password": "123456",
"errors": []
}
}
}
If method check_permissions returns False:
mutation {
accountRegister(username: "test", password: "123456") {
username
password
errors {
field
message
code
}
}
}
{
"data": {
"accountRegister": {
"username": null,
"password": null,
"errors": [
{
"field": null,
"message": "You do not have permission to perform this action",
"code": "PERMISSION_DENIED"
}
]
}
}
}
Documentation
Documentation and links to additional resources are available at https://mge-graphql.readthedocs.io/en/latest/
Release files for mge-graphql 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mge_graphql-1.1.0.tar.gz | 13.4 kB | Details |
Release files / mge_graphql-1.1.0.tar.gz
| Download URL | mge_graphql-1.1.0.tar.gz |
|---|---|
| Size | 13.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b28af9626159516101def98b8c318202029a864f1665b189f8000632d5ca1342
|
|
BLAKE2b-256 checksum How to use checksums |
bff000517d9f523eb5b88b0e11ef6e75d2357ff85503f039514820a25b2bc9bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|