Git Product home page Git Product logo

flask-graphql-rest's Introduction

Flask-GraphQL-REST

Flask extension to expose a GraphQL API as REST API

Build Status

Features

  • Generates RESTful endpoints for all query and mutation operations in a GraphQL schema
  • Supports GraphQL Relay

Installation

For installing flask-graphql-rest, just run this command in your shell

pip install "git+https://github.com/biosustain/flask-graphql-rest.git"

Examples

Here are two SQLAlchemy models

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func
from sqlalchemy.orm import backref

sa = SQLAlchemy(session_options={"autoflush": False})


class PublisherModel(sa.Model):
    __tablename__ = 'publisher'

    id = sa.Column(sa.Integer, primary_key=True)
    created_at = sa.Column(sa.DateTime(timezone=True), default=func.now(), nullable=False)
    name = sa.Column(sa.String(length=30))


class BookModel(sa.Model):
    __tablename__ = 'book'
    id = sa.Column(sa.Integer, primary_key=True)
    title = sa.Column(sa.String(length=30))
    publisher_id = sa.Column(sa.Integer,
                             sa.ForeignKey('publisher.id'),
                             nullable=True)

    # orm relationships
    publisher = sa.relationship(PublisherModel,
                                backref=backref('books', uselist=True),
                                uselist=False)

To create a GraphQL schema for it you simply have to write the following:

import graphene
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField

class Publisher(SQLAlchemyObjectType):
    class Meta:
        model = PublisherModel
        interfaces = (relay.Node,)


class Book(SQLAlchemyObjectType):
    class Meta:
        model = BookModel
        interfaces = (relay.Node,)


class Query(graphene.ObjectType):
    node = relay.Node.Field()
    books = SQLAlchemyConnectionField(Book)


schema = graphene.Schema(query=Query)

Then you can simply use flask-graphql-rest to create RESTful API from GraphQL schema:

from flask import Flask
from flask_graphql_rest import GraphQLREST


app = Flask(__name__)
graphene_rest = GraphQLREST(schema)
graphene_rest.init_app(app)

Now we can use HTTPie to test these newly created end points:

Fetch list of Book objects

http ":8005/books"
{
    "data": {
        "books": {
            "edges": [
                {
                    "cursor": "YXJyYXljb25uZWN0aW9uOjA=",
                    "node": {
                        "id": "Qm9vazox",
                        "publisher": {
                            "id": "UHVibGlzaGVyOjE="
                        },
                        "publisherId": 1,
                        "title": "Python Microservices Development"
                    }
                },
                {
                    "cursor": "YXJyYXljb25uZWN0aW9uOjE=",
                    "node": {
                        "id": "Qm9vazoy",
                        "publisher": {
                            "id": "UHVibGlzaGVyOjI="
                        },
                        "publisherId": 2,
                        "title": "Functional Programming in Python"
                    }
                }
            ],
            "pageInfo": {
                "endCursor": "YXJyYXljb25uZWN0aW9uOjE=",
                "hasNextPage": false,
                "hasPreviousPage": false,
                "startCursor": "YXJyYXljb25uZWN0aW9uOjA="
            }
        }
    }
}

Fetch a Book object by node id

http ":8005/node?id=Qm9vazox"
{
    "data": {
        "node": {
            "id": "Qm9vazox",
            "publisher": {
                "id": "UHVibGlzaGVyOjE="
            },
            "publisherId": 1,
            "title": "Python Microservices Development"
        }
    }
}

Fetch a Publisher object by node id

http ":8005/node?id=UHVibGlzaGVyOjE="
{
    "data": {
        "node": {
            "books": {
                "edges": [
                    {
                        "cursor": "YXJyYXljb25uZWN0aW9uOjA=",
                        "node": {
                            "id": "Qm9vazox"
                        }
                    }
                ],
                "pageInfo": {
                    "endCursor": "YXJyYXljb25uZWN0aW9uOjA=",
                    "hasNextPage": false,
                    "hasPreviousPage": false,
                    "startCursor": "YXJyYXljb25uZWN0aW9uOjA="
                }
            },
            "createdAt": "2017-10-12T07:42:15",
            "id": "UHVibGlzaGVyOjE=",
            "name": "Packt"
        }
    }
}

To learn more check out the following examples:

flask-graphql-rest's People

Contributors

lyschoening avatar pankaj28843 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.