question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trying to use graphene with arango-orm / python-arango and Graphene (relay)

See original GitHub issue

Hello,

I am just trying to use graphene (relay) with arangodb (using arango-orm + python-arango). I have the following :

in models_aDB.py:

from arango_orm.fields import String, Date
from arango_orm import Collection, Relation, Graph, GraphConnection
from arango_orm.references import relationship, graph_relationship
from datetime import datetime


class Department(Collection):
    __collection__ = 'department'
    _index = [{'type': 'hash', 'unique': False, 'fields': ['name']}]
    _key = String(required=True)  # registration number
    name = String(required=True, allow_none=False)
    # dob = Date()


class Role(Collection):
    __collection__ = 'role'
    _index = [{'type': 'hash', 'unique': False, 'fields': ['name']}]
    _key = String(required=True)  # registration number
    name = String(required=True, allow_none=False)
    # dob = Date()


class Employee(Collection):
    __collection__ = 'employee'
    _index = [{'type': 'hash', 'unique': False, 'fields': ['name']}]
    _key = String(required=True)  # registration number
    name = String(required=True, allow_none=False)
    department_key = String()
    role_key = String()
    hired_on = Date(default=datetime.now)
    department = relationship(Department, field='department_key')
    role = relationship(Role, field='role_key')

in schema.py:

import graphene
from graphene.relay import Node, ConnectionField, Connection
from resolvers.resolvers import get_role, get_department, get_employee


class Department(graphene.ObjectType):
    '''A Department'''
    class Meta:
        #model = DepartmentModel
        interfaces = (Node,)
    name = graphene.String(description='The Department')
    # _key = graphene.String()  # registration number

    @classmethod
    def get_node(cls, info, id):
        return get_department(id)


class Role(graphene.ObjectType):
    '''a Role'''
    class Meta:
        #model = RoleModel
        interfaces = (Node,)
    name = graphene.String(description='The Role')
    _key = graphene.String()  # registration number

    @classmethod
    def get_node(cls, info, id):
        return get_role(id)


class RoleConnection(Connection):
    class Meta:
        node = Role


class Employee(graphene.ObjectType):
    '''An Employee'''
    class Meta:
        #model = EmployeeModel
        interfaces = (Node, )
    name = graphene.String(description='The Employee')
    _key = graphene.String()  # registration number

    @classmethod
    def get_node(cls, info, id):
        return get_employee(id)


class EmployeeConnection(Connection):
    class Meta:
        node = Employee


class Query(graphene.ObjectType):
    node = Node.Field()
    all_employees = ConnectionField(EmployeeConnection)
    all_role = ConnectionField(RoleConnection)
    role = graphene.Field(Role)

schema = graphene.Schema(query=Query, types=[Department, Employee, Role])

in database.py:

from models_aDB import Department, Employee, Role
from arango import ArangoClient
from arango_orm import Database

#Arango
client = ArangoClient(protocol='http', host='xxxxxx', port=8529)
def init_arangodb():
    dev_db = client.db('zzz', username='xxxxx', password='yyyyy*', verify=True)
    db = Database(dev_db)

    if db.has_collection(Department):
        db.drop_collection(Department)
        db.create_collection(Department)
    else:
        db.create_collection(Department)

    if db.has_collection(Employee):
        db.drop_collection(Employee)
        db.create_collection(Employee)
    else:
        db.create_collection(Employee)

    if db.has_collection(Role):
        db.drop_collection(Role)
        db.create_collection(Role)
    else:
        db.create_collection(Role)

    # create the fixture
    engineering = Department(name='engineering')

    db.add(engineering)

    hr = Department(name='Human Resources')
    db.add(hr)

    manager = Role(name='manager')
    db.add(manager)

    engineer = Role(name='engineer')
    db.add(engineer)

    peter = Employee(name='Peter', role_key=engineer._key, department_key=engineering._key)
    db.add(peter)

    roy = Employee(name='Roy')
    roy.department = engineering
    roy.role = engineer
    db.add(roy)

    tracy = Employee(name='Tracy')
    tracy.role = manager
    tracy.department = hr
    db.add(tracy)

My resolver is currently like this (resolvers.py):

from models_aDB import Department, Employee, Role
from arango import ArangoClient
from arango_orm import Database

#Arango
client = ArangoClient(protocol='http', host='xxxxx', port=8529)

dev_db = client.db('dev', username="xxxxx", password="yyyy*", verify=True)
db = Database(dev_db)


def get_role(_key):
    return db.query(Role).by_key(_key)


def get_department(_key):
    return db.query(Department).by_key(_key)


def get_employee(_key):
    return db.query(Employee).by_key(_key)

and last in app.py:

from database import init_arangodb
from flask import Flask
from flask_graphql import GraphQLView
from schema import schema
from database import init_arangodb

app = Flask(__name__)
app.debug = True

default_query = '''
{
    allEmployees {
        edges {
            node {
                id,
                name,
                department {
                    id,
                    name
                },
                role {
                    id,
                    name
                }
            }
        }
    }
}               
'''.strip()

app.add_url_rule(
    '/graphql',
    view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True)
)

if __name__ == '__main__':
    init_arangodb()
    app.run(host='localhost', port=4000)

When I try to use graphqli, for example this 👍 {

    allEmployees {
        edges {
            node {
                id,
                name,
                department {
                    id,
                    name
                },
                role {
                    id,
                    name
                }
            }
        }
    }
}    

it results with errors.

can you help me to use graphene with arangodb ?

thanks for your help

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
M4k34B3tt3rW0r1Dcommented, May 29, 2019

Sorry. i fixed the typo

0reactions
jkimbocommented, Jun 24, 2019

Looks like your question has been resolved @langju so I’m going to close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the graphene.relay function in graphene - Snyk
Node, ) class Query(graphene.ObjectType): node = graphene.relay.Node.Field() users = graphene_sqlalchemy.SQLAlchemyConnectionField( User, active=graphene.
Read more >
Relay tutorial - Graphene-Python
In order to create this representation, Graphene needs to know about each type of object which will appear in the graph. This graph...
Read more >
[LIVE] Django GraphQL API with Python Graphene - YouTube
This is an edited livestream where I build a GraphQL API in Python's Django framework.It's a spaced repetition learning back-end to compare ...
Read more >
GraphQL Python Tutorial With Graphene (+ Django Integration)
In this graphql python tutorial, you will learn how to use graphene to build your own schema with python, and how to build...
Read more >
Enable PK based filtering in Django Graphene Relay while ...
I am using django-graphene with Relay on our GraphQL Server. The implementation imposes a Global ID requirement in the graphene.relay.Node class ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found