Skip to main content

Graphene resolvers

build status version python version wheel maintenance Conventional Commits

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

Install

pip install graphene-resolver

Usage

simple example:

import graphene
import graphene_resolver as resolver

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

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

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

relay node:

pets = [dict(
    id=1,
    name='pet1',
    age=1,
)]
class Pet(resolver.Resolver):
    schema = {
        'type': {
            'name': 'String',
            'age': 'Int',
        },
        'interfaces': (graphene.Node,)
    }

    def get_node(self, id_):
        return next(i for i in pets if i['id'] == int(id_))

    def validate(self, value):
        return (
            isinstance(value, typing.Mapping)
            and isinstance(value.get('name'), str)
            and isinstance(value.get('age'), int)
        )
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(resolver.Resolver):
    schema = {'name': 'String!'}

class Items(resolver.Resolver):
    schema = resolver.connection.get_type(Item)

    def resolve(self, **kwargs):
        return resolver.connection.resolve([{'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="
    }
  }
}

enum:

    class Foo(resolver.Resolver):
        schema = ('a', 'b')

        def resolve(self, **kwargs):
            return 'a'

    class Query(graphene.ObjectType):
        foo = Foo.as_field()

    schema = graphene.Schema(query=Query)
    assert str(schema) == '''\
schema {
  query: Query
}

enum Foo {
  a
  b
}

type Query {
  foo: Foo
}
'''

enum with description:

    class Foo(resolver.Resolver):
        schema = {
            'type': [('a', 'this is a'), ('b', 'this is b'), 'c'],
            'description': 'A enum',
        }

        def resolve(self, **kwargs):
            return 'a'

    class Query(graphene.ObjectType):
        foo = Foo.as_field()

    schema = graphene.Schema(query=Query)
    enum_type = schema.get_type('Foo')
    assert enum_type.description == 'A enum'
    assert enum_type.get_value('a').value == 'a'
    assert enum_type.get_value('a').description == 'this is a'
    assert enum_type.get_value('b').value == 'b'
    assert enum_type.get_value('b').description == 'this is b'
    assert enum_type.get_value('c').value == 'c'
    assert enum_type.get_value('c').description is None

union:

    class Foo(resolver.Resolver):
        schema = ({'a': 'String'}, {'b': 'Int'})

        def resolve(self, **kwargs):
            return {'__typename': 'Foo0', 'a': 'a'}

    class Query(graphene.ObjectType):
        foo = Foo.as_field()

    schema = graphene.Schema(query=Query)
    assert str(schema) == '''\
schema {
  query: Query
}

union Foo = Foo0 | Foo1

type Foo0 {
  a: String
}

type Foo1 {
  b: Int
}

type Query {
  foo: Foo
}
'''
{
  foo {
    __typename
    ... on Foo0 {
      a
    }
  }
}
{ "foo": { "__typename": "Foo0", "a": "a" } }

complicated example:

class Foo(resolver.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"] }]
  }
}

Metadata

Release files for graphene-resolver 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graphene-resolver 0.1.10
File Size Uploaded Provenance Actions
graphene-resolver-0.1.10.tar.gz 12.0 kB Without provenance Details for graphene-resolver-0.1.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for graphene-resolver 0.1.10
File Interpreter ABI Platform Actions
graphene_resolver-0.1.10-py3-none-any.whl Python 3 none any Details for graphene_resolver-0.1.10-py3-none-any.whl

Total release size: 25.5 kB

Release files / graphene-resolver-0.1.10.tar.gz

Download URL graphene-resolver-0.1.10.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4e98271d44c133701442df87e8ff684574f69887921d58e4c4a53b235c358703
BLAKE2b-256 checksum
How to use checksums
01e323d5dd197c84844fc8aa9dc70452fd726e110352448e356ba93d2c48b9e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.0 Windows/7

Release files / graphene_resolver-0.1.10-py3-none-any.whl

Download URL graphene_resolver-0.1.10-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22f4961f64ac5b1ea66b6c5cd85454d2e5d5ab21df60826545ba43cfacab6b73
BLAKE2b-256 checksum
How to use checksums
6ea19032228be2ad24823492f962e1a647efc006acc1f77ff504c99ca1b9a360
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.0 Windows/7

Security for graphene-resolver 0.1.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page