Package: ariadne-graphql-modules
Ariadne GraphQL ModulesAriadne package for implementing Ariadne GraphQL schemas using modular approach.For reasoning behind this work, please seethis GitHub discussion.SeeAPI referencefile for documentation.InstallationAriadne GraphQL Modules can be installed using pip:pip install ariadne-graphql-modulesAriadne 0.22 or later is required for library to work.ExamplesBasic examplefromdatetimeimportdatefromariadne.asgiimportGraphQLfromariadne_graphql_modulesimportObjectType,gql,make_executable_schemaclassQuery(ObjectType):__schema__=gql("""type Query {message: String!year: Int!}""")@staticmethoddefresolve_message(*_):return"Hello world!"@staticmethoddefresolve_year(*_):returndate.today().yearschema=make_executable_schema(Query)app=GraphQL(schema=schema,debug=True)Dependency injectionIf__schema__string contains other type, its definition should be provided via__requires__attribute:fromtypingimportList,Optionalfromariadne.asgiimportGraphQLfromariadne_graphql_modulesimportObjectType,gql,make_executable_schemafrommy_app.usersimportUser,get_user,get_last_usersclassUserType(ObjectType):__schema__=gql("""type User {id: ID!name: String!email: String}""")@staticmethoddefresolve_email(user:User,info):ifinfo.context["is_admin"]:returnuser.emailreturnNoneclassUsersQueries(ObjectType):__schema__=gql("""type Query {user(id: ID!): Userusers: [User!]!}""")__requires__=[UserType]@staticmethoddefresolve_user(*_,id:string)->Optional[User]:returnget_user(id=id)@staticmethoddefresolve_users(*_,id:string)->List[User]:returnget_last_users()# UsersQueries already knows about `UserType` so it can be omitted# in make_executable_schema argumentsschema=make_executable_schema(UsersQueries)app=GraphQL(schema=schema,debug=True)Deferred dependenciesOptionally dependencies can be declared as deferred so they can be provided directly tomake_executable_schema:fromtypingimportList,Optionalfromariadne.asgiimportGraphQLfromariadne_graphql_modulesimportDeferredType,ObjectType,gql,make_executable_schemafrommy_app.usersimportUser,get_user,get_last_usersclassUserType(ObjectType):__schema__=gql("""type User {id: ID!name: String!email: String}""")@staticmethoddefresolve_email(user:User,info):ifinfo.context["is_admin"]:returnuser.emailreturnNoneclassUsersQueries(ObjectType):__schema__=gql("""type Query {user(id: ID!): Userusers: [User!]!}""")__requires__=[DeferredType("User")]@staticmethoddefresolve_user(*_,id:string)->Optional[User]:returnget_user(id=id)@staticmethoddefresolve_users(*_,id:string)->List[User]:returnget_last_users()schema=make_executable_schema(UserType,UsersQueries)app=GraphQL(schema=schema,debug=True)Automatic case convertion betweenpython_worldandclientWorldResolving fields valuesUse__aliases__ = convert_caseto automatically set aliases for fields that convert casefromariadne_graphql_modulesimportObjectType,convert_case,gqlclassUserType(ObjectType):__schema__=gql("""type User {id: ID!fullName: String!}""")__aliases__=convert_caseConverting fields argumentsUse__fields_args__ = convert_caseon type to automatically convert field arguments to python case in resolver kwargs:fromariadne_graphql_modulesimportMutationType,convert_case,gqlfrommy_appimportcreate_userclassUserRegisterMutation(MutationType):__schema__=gql("""type Mutation {registerUser(fullName: String!, email: String!): Boolean!}""")__fields_args__=convert_case@staticmethodasyncdefresolve_mutation(*_,full_name:str,email:str):user=awaitcreate_user(full_name=full_name,email=email,)returnbool(user)Converting inputs fieldsUse__args__ = convert_caseon type to automatically convert input fields to python case in resolver kwargs:fromariadne_graphql_modulesimportInputType,MutationType,convert_case,gqlfrommy_appimportcreate_userclassUserRegisterInput(InputType):__schema__=gql("""input UserRegisterInput {fullName: String!email: String!}""")__args__=convert_caseclassUserRegisterMutation(MutationType):__schema__=gql("""type Mutation {registerUser(input: UserRegisterInput!): Boolean!}""")__requires__=[UserRegisterInput]@staticmethodasyncdefresolve_mutation(*_,input:dict):user=awaitcreate_user(full_name=input["full_name"],email=input["email"],)returnbool(user)Roots mergingQuery,MutationandSubscriptiontypes are automatically merged into one bymake_executable_schema:fromdatetimeimportdatefromariadne.asgiimportGraphQLfromariadne_graphql_modulesimportObjectType,gql,make_executable_schemaclassYearQuery(ObjectType):__schema__=gql("""type Query {year: Int!}""")@staticmethoddefresolve_year(*_):returndate.today().yearclassMessageQuery(ObjectType):__schema__=gql("""type Query {message: String!}""")@staticmethoddefresolve_message(*_):return"Hello world!"schema=make_executable_schema(YearQuery,MessageQuery)app=GraphQL(schema=schema,debug=True)Final schema will contain singleQuerytype thats result of merged tupes:typeQuery{message:String!year:Int!}Fields on final type will be ordered alphabetically.Moving declarations from AriadneAriadne GraphQL Modules support combining old and new approaches to schema definition.Seemoving guidefor examples and details.ContributingWe are welcoming contributions to Ariadne GraphQL Modules! If you've found a bug or issue, feel free to useGitHub issues. If you have any questions or feedback, please let us know viaGitHub discussions.Also make sure you follow@AriadneGraphQLon Twitter for latest updates, news and random musings!Crafted with ❤️ byMirumee Softwarehello@mirumee.com