Package: minos-router-graphql
minos-router-graphqlSummaryThis is graphQL plugin for Minos framework. This plugin integrates the officialgraphql-corelibrary. It is oriented to facilitate the development and better organize the graphql code.InstallationInstall the dependency:pipinstallminos-router-graphqlModifyconfig.ymlfile:...routers:-minos.plugins.graphql.GraphQlRouter...How to useDefine your business operationWe will use simple query for this demonstration:fromgraphqlimport(GraphQLString,)fromminos.networksimport(Request,Response,enroute,)classQueryService:@enroute.graphql.query(name="SimpleQuery",output=GraphQLString)defsimple_query(self,request:Request):...returnResponse("ABCD")Execute querySendpostrequest tohttp://your_ip_address:port/service_name/graphqlendpoint:{"query":"{ SimpleQuery }"}You will receive:{"data":{"SimpleQuery":"ABCD"},"errors":[]}That's all you need to make it work !For more information about graphql and how to define fields or structures, please see the officialgraphql-core. library.DecoratorsThere are 2 types of decorators, one forqueriesand one formutations(commands).@enroute.graphql.query(name="TestQuery",argument=GraphQLString,output=GraphQLString)deftest_query(self,request:Request):...returnResponnse(...)@enroute.graphql.command(name="TestCommand",argument=GraphQLString,output=GraphQLString)deftest_command(self,request:Request):...returnResponnse(...)Both decorators have the following arguments:name: The name of the query or commandargument[Optional]: The arguments it receives, if any.output: The expected output.ResolversAs you have seen above, the decorator does not specify the function that will resolve it.This is because it automatically takes the decorated function.In the following example:@enroute.graphql.query(name="TestQuery",argument=GraphQLString,output=GraphQLString)deftest_query(self,request:Request):...returnResponnse(...)The function in charge of resolving the query is the decorated functiontest_query.Queries (Query Service)Queries are used for a single purpose as their name indicates and that is to obtain information, that is, for queries.Base structure example:classQueryService:@enroute.graphql.query(name="TestQuery",argument=GraphQLString,output=GraphQLString)deftest_query(self,request:Request):...returnResponnse(...)More complex example:fromgraphqlimport(GraphQLBoolean,GraphQLField,GraphQLID,GraphQLInt,GraphQLNonNull,GraphQLObjectType,GraphQLString,)fromtypingimport(NamedTuple,Optional,)fromminos.networksimport(Request,Response,enroute,)user_type=GraphQLObjectType("UserType",{"id":GraphQLField(GraphQLNonNull(GraphQLID)),"firstName":GraphQLField(GraphQLNonNull(GraphQLString)),"lastName":GraphQLField(GraphQLNonNull(GraphQLString)),"tweets":GraphQLField(GraphQLInt),"verified":GraphQLField(GraphQLNonNull(GraphQLBoolean)),},)classUser(NamedTuple):"""A simple user object class."""firstName:strlastName:strtweets:Optional[int]id:Optional[str]=Noneverified:bool=FalseclassQueryService:@enroute.graphql.query(name="GetUser",argument=GraphQLInt,output=user_type)deftest_query(self,request:Request):id=awaitrequest.content()returnResponse(User(firstName="Jack",lastName="Johnson",tweets=563,id=str(id),verified=True))If you POST{service_name}/graphqlendpoint passing the query and variables:{"query":"query ($userId: Int!) { GetUser(request: $userId) {id firstName lastName tweets verified}}","variables":{"userId":3}}Yoy will receive:{"data":{"GetUser":{"id":"3","firstName":"Jack","lastName":"Johnson","tweets":563,"verified":true}},"errors":[]}Mutations (Command Service)Mutations are used to create, update or delete information.Base structure example:classCommandService:@enroute.graphql.command(name="TestQuery",argument=GraphQLString,output=GraphQLString)deftest_command(self,request:Request):...returnResponnse(...)More complex example:fromgraphqlimport(GraphQLBoolean,GraphQLField,GraphQLID,GraphQLInputField,GraphQLInputObjectType,GraphQLInt,GraphQLNonNull,GraphQLObjectType,GraphQLString,)fromtypingimport(NamedTuple,Optional,)fromminos.networksimport(Request,Response,enroute,)user_type=GraphQLObjectType("UserType",{"id":GraphQLField(GraphQLNonNull(GraphQLID)),"firstName":GraphQLField(GraphQLNonNull(GraphQLString)),"lastName":GraphQLField(GraphQLNonNull(GraphQLString)),"tweets":GraphQLField(GraphQLInt),"verified":GraphQLField(GraphQLNonNull(GraphQLBoolean)),},)user_input_type=GraphQLInputObjectType("UserInputType",{"firstName":GraphQLInputField(GraphQLNonNull(GraphQLString)),"lastName":GraphQLInputField(GraphQLNonNull(GraphQLString)),"tweets":GraphQLInputField(GraphQLInt),"verified":GraphQLInputField(GraphQLBoolean),},)classUser(NamedTuple):"""A simple user object class."""firstName:strlastName:strtweets:Optional[int]id:Optional[str]=Noneverified:bool=FalseclassCommandService:@enroute.graphql.command(name="CreateUser",argument=GraphQLNonNull(user_input_type),output=user_type)deftest_command(self,request:Request):params=awaitrequest.content()returnResponse(User(firstName=params["firstName"],lastName=params["lastName"],tweets=params["tweets"],id="4kjjj43-l23k4l3-325kgaa2",verified=params["verified"],))If you POST{service_name}/graphqlendpoint passing the query and variables:{"query":"mutation ($userData: UserInputType!) { CreateUser(request: $userData) {id, firstName, lastName, tweets, verified}}","variables":{"userData":{"firstName":"John","lastName":"Doe","tweets":42,"verified":true}}}Yoy will receive:{"data":{"CreateUser":{"id":"4kjjj43-l23k4l3-325kgaa2","firstName":"John","lastName":"Doe","tweets":42,"verified":true}},"errors":[]}Get SchemaBy calling{service_name}/graphql/schemawithGETmethod, you will receive the schema:"type Query {\n  GetUser(request: Int): UserType\n}\n\ntype UserType {\n  id: ID!\n  firstName: String!\n  lastName: String!\n  tweets: Int\n  verified: Boolean!\n}\n\ntype Mutation {\n  CreateUser(request: UserInputType!): UserType\n}\n\ninput UserInputType {\n  firstName: String!\n  lastName: String!\n  tweets: Int\n  verified: Boolean\n}"DocumentationThe official API Reference is publicly available at theGitHub Pages.Source CodeThe source code of this project is hosted at theGitHub Repository.Getting HelpFor usage questions, the best place to go to isStackOverflow.Discussion and DevelopmentMost development discussions take place over theGitHub Issues. In addition, aGitter channelis available for development-related questions.LicenseThis project is distributed under theMITlicense.