Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import importlib
import logging

from flask import Flask, request

from app.libs.graphene import load as load_graphene
import config


Expand All @@ -14,6 +14,7 @@ def create_app():
load_extensions(app)
load_models(app)
load_blueprints(app)
load_graphene(app)

return app

Expand Down
35 changes: 35 additions & 0 deletions app/libs/graphene/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import graphene
from flask_graphql import GraphQLView

from .user import User as UserType, CreateUser
from app.models.user import User


class Query(graphene.ObjectType):
hello = graphene.String()
user = graphene.Field(UserType, email=graphene.String())

def resolve_hello(self, args, context, info):
return 'World'

def resolve_user(self, args, context, info):
email = args.get('email')
return User.objects(email=email).first()


class Mutation(graphene.ObjectType):
create_user = CreateUser.Field()


schema = graphene.Schema(query=Query, mutation=Mutation)


def load(app):
app.add_url_rule(
'/graphql',
view_func=GraphQLView.as_view(
'graphql',
schema=schema,
graphiql=True
)
)
38 changes: 38 additions & 0 deletions app/libs/graphene/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import graphene
from app.models.user import User as UserModel


class User(graphene.ObjectType):
email = graphene.String()
first_name = graphene.String()
last_name = graphene.String()
id = graphene.String()

def resolve_id(self, args, context, info):
return str(self.id)


class CreateUserInput(graphene.InputObjectType):
email = graphene.String()
first_name = graphene.String()
last_name = graphene.String()
password = graphene.String()


class CreateUser(graphene.Mutation):
class Input:
input = graphene.Argument(CreateUserInput)

ok = graphene.Boolean()
user = graphene.Field(User)

@staticmethod
def mutate(root, args, context, info):
user = UserModel.register(**args['input'])
user = User(
email=user.email,
first_name=user.first_name,
last_name=user.last_name
)
ok = True
return CreateUser(user=user, ok=ok)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ flask-mongoengine
flask-wtf
gunicorn
boto3
Flask-GraphQL
graphene