Coder Social home page Coder Social logo

graphene-peewee-async's Introduction

graphene-peewee-async

graphene + peewee-async integration ❤️

Features

  • Querying
    • Fields selection (considered by SELECT statement)
    • Related entities subselection (using foreign key joins)
    • Filters (django-style lookups, like peewee.SelectQuery.filter args)
    • Order (multiple fields, asc/dsc support)
    • Pagination (page, paginate_by support plus unpaginated total count auto-fetching)
  • Mutations (both single object and bulk operating, filtering just like for querying)
    • Create
    • Update
    • Delete
    • Clone

Usage sample

# Define models

class Author(Model):
    name = CharField()
    rating = IntegerField()

class Book(Model):
    name = CharField()
    year = IntegerField()
    author = ForeignKeyField(Author)

# Create nodes

class BookNode(PeeweeObjectType):
    class Meta:
        model = Book
        manager = db_manager

class AuthorNode(PeeweeObjectType):
    class Meta:
        model = Author
        manager = db_manager

# Create connections

class BookConnection(PeeweeConnection):
    class Meta:
        node = BookNode

# Aggregate queries

class Query(ObjectType):
    books = PeeweeConnectionField(BookConnection)

# Create schema

schema = Schema(query=Query, auto_camelcase=False)

# Execute graphql query

result = schema.execute('''
    query {
        books (filters: {author__name__ilike: "%Lovecraft%"}) {
            total
            edges {
                node {
                    id
                    name
                    author {
                        id
                        name
                    }
                }
            }
        }
    }''',
    return_promise=True,
    executor=AsyncioExecutor()
)

# Await result if required (failed queries are usually returning result
#                           synchronously with non-empty `result.errors`
#                           while successful ones requires awaiting
#                           of peewee/DB level queries of course)

if not isinstance(result, ExecutionResult):
    result = await result

# Enjoy the result :)

print(result.data)
#
# ===>
#
# {'books': {
#     'total': 2,
#     'edges': [
#         {'node': {
#             'id': 5,
#             'name': 'Dagon',
#             'author': {
#                 'id': 1,
#                 'name': 'Howard Lovecraft'
#             }
#         }},
#         {'node': {
#             'id': 6,
#             'name': 'At the Mountains of Madness',
#             'author': {
#                 'id': 1,
#                 'name': 'H.P. Lovecraft'
#             }
#         }}
#     ]
# }}

Advanced usage

Be sure to check API tests for advanced query/mutation usages and auto-generating such schema for them.

Install

Install as package:

pip3 install graphene-peewee-async

graphene-peewee-async's People

Contributors

insolite avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.