Skip to main content

Transform SQLAlchemy Table into Graphene ObjectType.

Project description

graphene-objecttype-from-sqlalchemy-table

https://img.shields.io/pypi/v/graphene_objecttype_from_sqlalchemy_table.svg https://travis-ci.org/Joko013/graphene-objecttype-from-sqlalchemy-table.svg?branch=master

Transform SQLAlchemy Table into Graphene ObjectType.

  • Free software: MIT license

Features

The purpose of this library is to allow a lightweight GraphQL schema creation based on database table definitions when full SQLAlchemy ORM cannot be used. It converts Table columns into their Graphene counterparts and creates the corresponding Graphene ObjectType to be used as a schema field.

Note that if your application uses SQLAlchemy ORM, graphene-sqlalchemy still offers more features and you could benefit from using it.

Installation

In shell run:

pip install graphene-objecttype-from-sqlalchemy-table

Example usage

Let’s start by defining a mock database table.

import sqlalchemy as sa

t = sa.Table(
    'test_table',
    sa.MetaData(),
    sa.Column('foo', sa.String, doc='This will be passed to field description.'),
    sa.Column('bar', sa.Integer),
)

Then create a Graphene object type using a Meta class with the reference to the SQLAlchemy table.

from graphene_objecttype_from_sqlalchemy_table import ObjectTypeFromTable

class TestTable(ObjectTypeFromTable):
    class Meta:
        table = t

And finally define the GraphQL query structure using the new object type.

from graphene import Field, ObjectType, Schema

class Query(ObjectType):
    test_table = Field(TestTable)

    # custom resolver for the table (probably a database query in a real world application)
    # returns a dictionary or an object with attributes corresponding to column names
    def resolve_test_table(root, info):
        return {'foo': 'Hello world', 'bar': 42}

schema = Schema(query=Query)

# confirm that the query resolves correctly
schema.execute('query { testTable { foo, bar } }')
# {'data': {'testTable': {'foo': 'Hello world', 'bar': 42}}}

It is possible to exclude specific columns from the resulting object by listing them as excluded_columns or to select only a subset of columns with only_selected_columns attribute.

class TestTable(ObjectTypeFromTable):
    class Meta:
        table = t
        excluded_columns = ('foo', )
        # only_selected_columns = ('bar', )

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.1.2 (2020-09-02)

  • No changes, Travis CI encryption issues caused incorrect pypi release, pypi doesn’t allow deleting and re-releasing the same version hence a new version is needed to fix this.

0.1.1 (2020-08-31)

  • Fix duplicate enum types error when multiple columns reuse one SQLAlchemy enum or when enum types are used as field arguments.

0.1.0 (2020-08-17)

  • First release on PyPI.

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

Built Distribution

File details

Details for the file graphene_objecttype_from_sqlalchemy_table-0.1.2.tar.gz.

File metadata

  • Download URL: graphene_objecttype_from_sqlalchemy_table-0.1.2.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0

File hashes

Hashes for graphene_objecttype_from_sqlalchemy_table-0.1.2.tar.gz
Algorithm Hash digest
SHA256 6d192437924211f08eb453398a0351f3caad981972dc5b401ed5f4367e4b80b0
MD5 979d54685bbacf7955a70efce9c9d070
BLAKE2b-256 399d9b91d7feb62cfa15124c20055f098da903a85d7acb0ee184430f07aa7620

See more details on using hashes here.

File details

Details for the file graphene_objecttype_from_sqlalchemy_table-0.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for graphene_objecttype_from_sqlalchemy_table-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 83b6392d3e63b796172fa62272692f1c1882598b3daade9731adae45fae45d0e
MD5 60c73b9256b8960cbe927ad623184a14
BLAKE2b-256 9d6a6b420d92069994cdc6c337742409e3cd26cb76b89a1ea16bba26d8f71ebc

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