Git Product home page Git Product logo

graphql-pynamodb's Introduction

Graphene-PynamoDB Build Status Coverage Status PyPI version

A PynamoDB integration for Graphene.

Installation

For instaling graphene, just run this command in your shell

pip install graphene-pynamodb

Examples

Here is a simple PynamoDB model:

from uuid import uuid4
from pynamodb.attributes import UnicodeAttribute
from pynamodb.models import Model


class User(Model):
    class Meta:
        table_name = "my_users"
        host = "http://localhost:8000"

    id = UnicodeAttribute(hash_key=True)
    name = UnicodeAttribute(null=False)


if not User.exists():
    User.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)
    User(id=str(uuid4()), name="John Snow").save()
    User(id=str(uuid4()), name="Daenerys Targaryen").save()

To create a GraphQL schema for it you simply have to write the following:

import graphene
from graphene_pynamodb import PynamoObjectType


class UserNode(PynamoObjectType):
    class Meta:
        model = User
        interfaces = (graphene.Node,)


class Query(graphene.ObjectType):
    users = graphene.List(UserNode)

    def resolve_users(self, args, context, info):
        return list(User.scan())


schema = graphene.Schema(query=Query)

Then you can simply query the schema:

query = '''
    query {
      users {
        name
      }
    }
'''
result = schema.execute(query)

To learn more check out the following examples:

Limitations

graphene-pynamodb includes a basic implementation of relationships using lists. OneToOne and OneToMany relationships are serialized as a List of the ids and unserialized lazyly. The limit for an item's size in DynamoDB is 400KB (see http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html) This means the total "row" size including the serialized relationship needs to fit within 400KB so make sure to use this accordingly.

In addition, scan operations on DynamoDB are unsorted by design. This means that there is no reliable way to get a paginated result (Cursor support) on a root PynamoConnectionField.

This means that if you need to paginate items, it is best to have them as a OneToMany relationship inside another Field (usually viewer or node).

Contributing

After cloning this repo, ensure dependencies are installed by running:

python setup.py install

After developing, the full test suite can be evaluated by running:

python setup.py test # Use --pytest-args="-v -s" for verbose mode

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.