Package: newql
NewQLA simplified GraphQL-esque libraryNotable features:Schema defined using pythonic classesIntrospection for support with the GraphiQL interfaceField / type / argument docstringsNested typesAliasesQuery variablesEnums (can simply use pure python enums)Flexible type system (unlike GraphQL which coerces and validates more strictly)WhatNewQLdoes not support:FragmentsDirectivesSubscriptionsPer-field or per-type introspection (the introspection returns all or nothing)InstallationRequires python 3.7+pipinstallnewqlUsagefromnewqlimportExecutionContext,Schema,fieldclassProduct:"""Represents a product in stock"""product_id=field(type=int)product_name=field(type=str)price=field(type=float)classProductQuery:# pass 'type' here since the function returns a dictionary,# but we want to actually resolve to a Product@field(type=Product)defproduct(_,context:ExecutionContext,product_id:int)->dict:# The "Args" section of the docstring is parsed out to add# docs to the arguments (can be seen in GraphiQL)"""Find a specific product by idArgs:product_id: The ID of the product to find"""products={1:("Product1",49.99),2:("Product2",94.49)}ifproduct_idinproducts:name,price=products[product_id]return{"product_id":product_id,"product_name":name,"price":price}else:raiseValueError(f"Product not found:{product_id}")schema=Schema(ProductQuery)Explore the above example usingpython -m newql.dev_server --schema newql.example.product_schema:{product(product_id:1){priceproduct_name}}Or execute a query through code:QUERY="""{product(product_id: 1) {priceproduct_name}}"""fromnewql.exampleimportproduct_schemaproduct_schema.execute(QUERY)# => {"data": {"product": {"price": 49.99, "product_name": "Product1"}}}A mutation class can be defined in exactly the same way as a query class,
and can be passed toSchemaas a second positional argument or by themutationkeyword argument.classMutation:...schema=Schema(mutation=Mutation)To start a dev server serving a GraphiQL interface:python-mnewql.dev_server# uses the schema in newql.example# can specify a custom schemapython-mnewql.dev_server--schema<fullimportnameofyourschema># for example:python-mnewql.dev_server--schemamy_module.nested_module.my_schemaNote that when defining a field inline and not specifying the field name,
the field name will be determined by the name of the variable to which it
is assigned. For example:classMyClass:my_field=field(type=str)The field will be namedmy_field. This is achieved by the libraryvarname,
however since it needs to parse AST to determine the name, it can take time.If there are more than a couple hundred fields, it is recommended to explicitly
set the name of the field to avoid this performance hit:classMyClass:my_field=field("my_field",type=str)DevelopmentClone the repo, then from the project directory:python3-mvenv.venv
..venv/bin/activate

makeinstall-devTo run tests (and show coverage):maketestBefore making changes, fix formatting and check changes (isort, black, flake8, mypy):makeformatcheckChangelog0.6.0 2021-08-06Make errors in the response return a list to be up to spec with GraphQL0.5.0 2021-07-26Allow variable types to be a list, e.g.[String!]0.4.0 2021-07-23Allow '!' after variable type definitions (currently just ignored)0.3.1 2021-07-22Allow unused variables to be sent in query0.3.0 2021-07-21Support commas as whitespace0.2.0 2021-07-21Improve Enum support (was not working properly when wrapped
in an Optional or List)0.1.1 2021-06-30Fix changelog release date + headerCosmetic improvements to query parsing0.1.0 2021-06-30Initial release