All Projects → davidyapdy → sanic-graphql-example

davidyapdy / sanic-graphql-example

Licence: other
Sanic using Graphsql + SQLAlchemy example

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to sanic-graphql-example

fastapi-debug-toolbar
A debug toolbar for FastAPI.
Stars: ✭ 90 (+328.57%)
Mutual labels:  sqlalchemy, graphene
graphene-sqlalchemy-filter
Filters for Graphene SQLAlchemy integration
Stars: ✭ 117 (+457.14%)
Mutual labels:  sqlalchemy, graphene
CourseCake
By serving course 📚 data that is more "edible" 🍰 for developers, we hope CourseCake offers a smooth approach to build useful tools for students.
Stars: ✭ 21 (+0%)
Mutual labels:  sqlalchemy, graphene
hypothesis sqlalchemy
hypothesis strategies for generating SQLAlchemy objects
Stars: ✭ 24 (+14.29%)
Mutual labels:  sqlalchemy
flask-template
Template for creating Flask based projects
Stars: ✭ 60 (+185.71%)
Mutual labels:  sqlalchemy
favv
Fullstack Web Application Framework With FastAPI + Vite + VueJS. Streamlit for rapid development.
Stars: ✭ 17 (-19.05%)
Mutual labels:  sqlalchemy
pyramid basemodel
Global base classes for Pyramid SQLAlchemy applications.
Stars: ✭ 14 (-33.33%)
Mutual labels:  sqlalchemy
OpenAlchemy
Define SQLAlchemy models using the OpenAPI specification.
Stars: ✭ 39 (+85.71%)
Mutual labels:  sqlalchemy
fastql
⚙️ Full stack, Modern Web Application Generator. ✨ Using FastAPI, GraphQL, PostgreSQL as database, Docker, automatic HTTPS and more. 🔖
Stars: ✭ 80 (+280.95%)
Mutual labels:  graphene
microAuth
A fast, documented, and tested python3 API boilerplate
Stars: ✭ 24 (+14.29%)
Mutual labels:  sanic
robot-mind-meld
A little game powered by word vectors
Stars: ✭ 31 (+47.62%)
Mutual labels:  sanic
sanic-plugin-toolkit
Easily create Plugins for Sanic!
Stars: ✭ 49 (+133.33%)
Mutual labels:  sanic
Qwerkey
Qwerkey is a social media platform for connecting and learning more about mechanical keyboards built on React and Redux in the frontend and Flask in the backend on top of a PostgreSQL database.
Stars: ✭ 22 (+4.76%)
Mutual labels:  sqlalchemy
sqlalchemy-adapter
SQLAlchemy Adapter for PyCasbin
Stars: ✭ 53 (+152.38%)
Mutual labels:  sqlalchemy
alchemy-mock
SQLAlchemy mock helpers.
Stars: ✭ 74 (+252.38%)
Mutual labels:  sqlalchemy
magql
The magical GraphQL framework that generates an API for your data.
Stars: ✭ 26 (+23.81%)
Mutual labels:  sqlalchemy
ChefAPI
API using FastAPI and PostgreSQL for sharing or keeping track of awesome food recipes Based on Oauth2 and JWT 💎
Stars: ✭ 16 (-23.81%)
Mutual labels:  sqlalchemy
shillelagh
Making it easy to query APIs via SQL
Stars: ✭ 172 (+719.05%)
Mutual labels:  sqlalchemy
Flask-Validator
Validator for SQLAlchemy Models
Stars: ✭ 27 (+28.57%)
Mutual labels:  sqlalchemy
swap
A Solver for the Wavelength Assignment Problem (RWA) in WDM networks
Stars: ✭ 27 (+28.57%)
Mutual labels:  sqlalchemy

Example Sanic + SQLAlchemy + Graphene (Graphql) Project

This example project demos integration between Graphene, Sanic and SQLAlchemy. The project contains two models, one named Department, Role and another named Employee.

sanic-graphql-example

Sanic example using Graphsql + SQLAlchemy

Run app.py and access via localhost:5000/graphql

Required files

  1. Graphene
  2. Sanic
  3. Graphene-Sqlalchemy
  4. Sqlalchemy

#Tutzz

  1. Build your model first
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String, func
from sqlalchemy.orm import backref, relationships

from database import Base


# Basic SQLAlchemy
class Department(Base):
    __tablename__ = 'department'
    id = Column(Integer, primary_key=True)
    name = Column(String)


class Role(Base):
    __tablename__ = 'roles'
    role_id = Column(Integer, primary_key=True)
    name = Column(String)


class Employee(Base):
    __tablename__ = 'employees'
    employee_id = Column(Integer, primary_key=True)
    name = Column(String)
    hired_on = Column(DateTime, default=func.now())
    department_id = Column(Integer, ForeignKey('department.id'))
    role_id = Column(Integer, ForeignKey('roles.role_id'))
    department = relationships(
        Department,
        backref=backref('employee',
                        uselist=True,
                        cascade='delete,all'))
    role = relationships(
        Role,
        backref=backref('roles',
                        uselist=True,
                        cascade='delete,all'))
  1. Initiate database + seed data + Commit
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker

# Create database
engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
                                         autoflush=False,
                                         bind=engine))

Base = declarative_base()
Base.query = db_session.query_property()


async def init_db():
    from models import Department, Employee, Role
    Base.metadata.drop_all(bind=engine)
    Base.metadata.create_all(bind=engine)

    # Create Fixture for department
    engineering = Department(name='Engineering')
    db_session.add(engineering)
    hr = Department(name='Human Resource')
    db_session.add(hr)

    # Create Fixture for Role
    manager = Role(name='manager')
    db_session.add(manager)
    engineer = Role(name='Engineer')
    db_session.add(engineer)

    # Create
    peter = Employee(name='Peter', department=engineering, role=manager)
    db_session.add(peter)
    roy = Employee(name='Roy', department=engineering, role=engineer)
    db_session.add(roy)
    tracy = Employee(name='Tracy', department=hr, role=manager)
    db_session.add(tracy)

    # Insert seed data into database
    db_session.commit()
  1. Define your scheme for graphql It is important to know some important terms here like SQLAlchemyConnectionField and relay.Node. Check out graphene official documentation for more info
import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyConnectionField, SQLAlchemyObjectType
from models import Department as DepartmentModel
from models import Employee as EmployeeModel
from models import Role as RoleModel


class Department(SQLAlchemyObjectType):
    class Meta:
        model = DepartmentModel
        interfaces = (relay.Node, )


class Employee(SQLAlchemyObjectType):
    class Meta:
        model = EmployeeModel
        interfaces = (relay.Node, )


class Role(SQLAlchemyObjectType):
    class Meta:
        model = RoleModel
        interfaces = (relay.Node, )


class Query(graphene.ObjectType):
    node = relay.Node.Field()
    all_employee = SQLAlchemyConnectionField(Employee)
    all_roles = SQLAlchemyConnectionField(Role)
    role = graphene.Field(Role)


schema = graphene.Schema(query=Query, types=[Department, Employee, Role])
  1. Let's run our Sanic App
from sanic_graphql import GraphQLView
from sanic import Sanic
from database import db_session, init_db
from schema import schema

app = Sanic(__name__)
app.debug = True

# This will be the query
default_query = """
{
    allEmployee {
        edges {
            node {
                id,
                name,
                department {
                    id,
                    name
                },
                role {
                    id,
                    name
                }
            }
        }
    }
}
""".strip()

# It's like HTTP routes but instead, it directs you to /graphql (Schema).
# graphiq = True will initiate the GUI
app.add_route(GraphQLView.as_view(schema=schema, graphiql=True), '/graphql')

# runs the app and database. init_db contains seed data.
if __name__ == '__main__':
    init_db()
    app.run()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].