Package: python-gql
python-gqlPython schema-first GraphQL library based on GraphQL-core.RequirementsPython 3.7+Installationpip install python-gqlGetting startimportgraphqlfromgqlimportgql,make_schema,query,mutatetype_defs=gql("""type Query {hello(name: String!): String!}type Post {author: String!comment: String!}type Mutation {addPost(author: String, comment: String): Post!}""")@querydefhello(parent,info,name:str)->str:returnname@mutatedefadd_post(parent,info,author:str=None,comment:str=None)->dict:return{'author':author,'comment':comment}schema=make_schema(type_defs)q="""query {hello(name: "graphql")}"""result=graphql.graphql_sync(schema,q)print(result.data)# result: {'hello': 'graphql'}q="""mutation {addPost(author: "syfun", comment: "This is a good library.") {authorcomment}}"""result=graphql.graphql_sync(schema,q)print(result.data)# result: {'addPost': {'author': 'syfun', 'comment': 'This is a good library.'}}Build schemaThis library isschema-first, so you must build a schema explicitly.Here, we have two methods to build a schema, bya type definitionsora schema file.fromgqlimportgql,make_schematype_defs=gql("""type Query {hello(name: String!): String!}""")schema=make_schema(type_defs)gqlfunction will check your type definitions syntax.fromgqlimportmake_schema_from_fileschema=make_schema_from_file('./schema.graphql')Resolver decoratorsIn Python,decoratoris my favorite function, it save my life!We can usequery,mutation,subscribeto bind functions to GraphQL resolvers.@querydefhello(parent,info,name:str)->str:returnnameThese decorators will auto convert the snake function to camel one.# add_port => addPost@mutatedefadd_post(parent,info,author:str=None,comment:str=None)->dict:return{'author':author,'comment':comment}When the funcation name different from the resolver name, you can give a name argument to these decorators.@query('hello')defhello_function(parent,info,name:str)->str:returnnameAboutsubscribe, please seegql-subscriptions.Enum type decoratorUseenum_typedecorator with a python Enum class.fromenumimportEnumfromgqlimportenum_type@enum_typeclassGender(Enum):MALE=1FEMALE=2Custom ScalarUsescalar_typedecorator with a python class.fromgqlimportscalar_type@scalar_typeclassJSONString:description="The `JSONString` represents a json string."@staticmethoddefserialize(value:Any)->str:returnjson.dumps(value)@staticmethoddefparse_value(value:Any)->dict:ifnotisinstance(value,str):raiseTypeError(f'JSONString cannot represent non string value:{inspect(value)}')returnjson.loads(value)@staticmethoddefparse_literal(ast,_variables=None):ifisinstance(ast,StringValueNode):returnjson.loads(ast.value)returnINVALIDCustom directivefromgqlimportgql,make_schema,query,SchemaDirectiveVisitorfromgql.resolverimportdefault_field_resolvertype_defs=gql("""directive @upper on FIELD_DEFINITIONtype Query {hello(name: String!): String! @upper}""")classUpperDirective(SchemaDirectiveVisitor):defvisit_field_definition(self,field,object_type):original_resolver=field.resolveordefault_field_resolverdefresolve_upper(obj,info,**kwargs):result=original_resolver(obj,info,**kwargs)ifresultisNone:returnNonereturnresult.upper()field.resolve=resolve_upperreturnfieldschema=make_schema(type_defs,directives={'upper':UpperDirective})Apollo FederationExampleApollo FederationThanks toAriadneFramework supportStarlette GraphQLDjango GraphQL