Package: mge-graphql
MGE-GraphQLIntroductionMGE-GraphQLis a Python library for building GraphQL mutations fast and easily.Data Validations:A similar data validation workflow as Django.Errors:Support for throwing errorsPermissions:Support for user permissionsInstallationFor instaling MGE-GraphQL, just run this command in your shellpipinstall"mge-graphql"ExamplesHere is one example for you to get started:
Createerror_codes.pyand define some errorsfromenumimportEnumfrommge_graphql.utils.error_codesimport(MGE_ERROR_CODE_ENUMS,generate_error_codes)classAccountErrorCode(Enum):# Here you define your Error CodesINVALID_PASSWORD="invalid_password"# Register error codesMGE_ERROR_CODE_ENUMS.append(AccountErrorCode)generate_error_codes()Createenums.pyto define your Graphene Error Enumsimportgrapheneimporterror_codesasaccount_error_codes# Create Graphene EnumAccountErrorCode=graphene.Enum.from_enum(account_error_codes.AccountErrorCode)Createtypes.pyto create your custom error graphql object typefrommge_graphql.types.commonimportErrorfromenumsimportAccountErrorCodeclassAccountError(Error):# Custom fields# Support for error_codecode=AccountErrorCode(description="The error code.",required=True)Createmutations.pyto create your first mutationfrommge_graphql.mutations.baseimportBaseMutationfrommge_graphql.exceptionsimportValidationErrorfromtypesimportAccountErrorCodeimportgrapheneclassAccountRegister(BaseMutation):# YOUR GRAPHENE FIELDSusername=graphene.String(required=True)password=graphene.String(required=True)classArguments:username=graphene.String(required=True)password=graphene.String(required=True)classMeta:description="Register a new account."# Set our custom AccountError classerror_type_class=AccountError@classmethoddefclean_password(cls,password,errors):iflen(password)<6:errors["password"].append(ValidationError({"password":ValidationError("Password cannot be less than 6 characters.",code=AccountErrorCode.INVALID_PASSWORD)}))returnpassword@classmethoddefclean(cls,**data):errors=defaultdict(list)cls.clean_password(data["password"],errors)iferrors:raiseValidationError(errors)returndata@classmethoddefcheck_permissions(cls,context):# Permission Checks.# If False, then it will raise an Permission Denied ErrorreturnTrue@classmethoddefperform_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 LogicreturnAccountRegister(username=cleaned_username,password=cleaned_password)Createschema.pyand register your mutation:frommutationsimportAccountRegisterimportgrapheneclassMutation(graphene.ObjectType):account_register=AccountRegister.Field()schema=graphene.Schema(mutation=Mutation)And.. we are done! Let's try our mutationinvalid input:mutation{accountRegister(username:"test",password:"234"){usernamepassworderrors{fieldmessagecode}}}{"data":{"accountRegister":{"username":null,"password":null,"errors":[{"field":"password","message":"Passwordcannotbelessthan6characters.","code":"INVALID_PASSWORD"}]}}}valid input:mutation{accountRegister(username:"test",password:"123456"){usernamepassworderrors{fieldmessagecode}}}{"data":{"accountRegister":{"username":"test","password":"123456","errors":[]}}}If methodcheck_permissionsreturns False:mutation{accountRegister(username:"test",password:"123456"){usernamepassworderrors{fieldmessagecode}}}{"data":{"accountRegister":{"username":null,"password":null,"errors":[{"field":null,"message":"Youdonothavepermissiontoperformthisaction","code":"PERMISSION_DENIED"}]}}}DocumentationDocumentation and links to additional resources are available athttps://mge-graphql.readthedocs.io/en/latest/