Skip to main content

Tools for use [`graphene-django`](https://github.com/graphql-python/graphene-django)

Project description

Graphene django tools

build status version python version django version wheel maintenance

Tools for use graphene-django

Install

pip install graphene-django-tools

Features

Resolver

Using mongoose-like schema to write apollo-like resolver.

simple example:

import graphene
import graphene_django_tools as gdtools

class Foo(gdtools.Resolver):
    schema = {
        "args": {
            "key":  'String!',
            "value": 'String!',
        },
        "type": 'String!',
    }

    def resolve(self, **kwargs):
        self.parent # parent field
        self.info # resolve info
        self.context # django request object
        return kwargs['value']

class Query(graphene.ObjectType):
    foo = Foo.as_field()
{
  foo(key: "k", value: "v")
}
{ "foo": "v" }

relay node:

class Pet(gdtools.Resolver):
    schema = {
        'type': {
            'name': models.Pet._meta.get_field('name'),
            'age': models.Pet._meta.get_field('age'),
        },
        'interfaces': (graphene.Node,)
    }

    def get_node(self, id_):
        return models.Pet.objects.get(pk=id_)

    def validate(self, value):
        return isinstance(value, models.Pet)

class Query(graphene.ObjectType):
    node = graphene.Node.Field()

schema = graphene.Schema(query=Query, types=[Pet.as_type()])
{
  node(id: "UGV0OjE=") {
    id
    __typename
    ... on Pet {
      name
      age
    }
  }
}
{ "node": { "id": "UGV0OjE=", "__typename": "Pet", "name": "pet1", "age": 1 } }

relay connection:

class Item(gdtools.Resolver):
    schema = {'name': 'String!'}

class Items(gdtools.Resolver):
    schema = gdtools.get_connection(Item)

    def resolve(self, **kwargs):
        return gdtools.resolve_connection([{'name': 'a'}, {'name': 'b'}], **kwargs)
{
  items {
    edges {
      node {
        name
      }
      cursor
    }
    pageInfo {
      total
      hasNextPage
      hasPreviousPage
      startCursor
      endCursor
    }
  }
}
{
  "items": {
    "edges": [
      { "node": { "name": "a" }, "cursor": "YXJyYXljb25uZWN0aW9uOjA=" },
      { "node": { "name": "b" }, "cursor": "YXJyYXljb25uZWN0aW9uOjE=" }
    ],
    "pageInfo": {
      "total": 2,
      "hasNextPage": false,
      "hasPreviousPage": false,
      "startCursor": "YXJyYXljb25uZWN0aW9uOjA=",
      "endCursor": "YXJyYXljb25uZWN0aW9uOjE="
    }
  }
}

complicated example:

class Foo(gdtools.Resolver):
    _input_schema = {
        "type": {"type": 'String'},
        "data": [
            {
                "type":
                {
                    "key": {
                        "type": 'String',
                        "required": True,
                        "description": "<description>",
                    },
                    "value": 'Int',
                    "extra": {
                        "type": ['String!'],
                        "deprecation_reason": "<deprecated>"
                    },
                },
                "required": True
            },
        ],
    }
    schema = {
        "args": {
            "input": _input_schema
        },
        "type": _input_schema,
        "description": "description",
        "deprecation_reason": None
    }

    def resolve(self, **kwargs):
        return kwargs['input']
{
  foo(
    input: { type: "type", data: [{ key: "key", value: 42, extra: ["extra"] }] }
  ) {
    type
    data {
      key
      value
      extra
    }
  }
}
{
  "foo": {
    "type": "type",
    "data": [{ "key": "key", "value": 42, "extra": ["extra"] }]
  }
}

Query

  • ModelField
  • ModelConnectionField
  • ModelFilterConnectionField

example schema

Map the user model with filter in 10 lines.

Mutation

  • ModelMutation
  • ModelCreateMutation
  • ModelUpdateMutation

example: graphene_django_tools.auth module

Map the user model with password validation in 40 lines.

Re-implemented Mutation class

Supports arguments on interface.

class ClientMutationID(graphene.Interface):
    """Mutation with a client mutation id.  """

    class Arguments:
        client_mutation_id = graphene.String()

    client_mutation_id = graphene.String()

Data loader integrate

Enable by add 'graphene_django_tools.dataloader.middleware.DataLoaderMiddleware' to your django settings GRAPHENE['MIDDLEWARE']

Development

run dev server: make dev

test: make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphene_django_tools-0.19.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

graphene_django_tools-0.19.0-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file graphene_django_tools-0.19.0.tar.gz.

File metadata

  • Download URL: graphene_django_tools-0.19.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.0 Windows/10

File hashes

Hashes for graphene_django_tools-0.19.0.tar.gz
Algorithm Hash digest
SHA256 0c5f704f3d715c0b87583061d38f98fdd77d6fee50f34b84701b262e536dd5d3
MD5 2828c0080ae017efb4a18f4523e871a2
BLAKE2b-256 ef947f2fa11f58d9a5e6176a8247e2001d2c8f7706d65f0c1998f3db45e7cea2

See more details on using hashes here.

File details

Details for the file graphene_django_tools-0.19.0-py3-none-any.whl.

File metadata

File hashes

Hashes for graphene_django_tools-0.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a2d6a3c4692a3b4600de84d84aab905acf0259642ae30477d512c7a5c3ec333
MD5 e55d029cbb81d36bdd3d7b0d486b8a6c
BLAKE2b-256 7422ee9e2b336b3daec24f84206fbf28050f0b50c87cfc1dde83a6a300b762eb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page