From 497a15892cb55f05ab7cabcb8d2e6ad255214c4b Mon Sep 17 00:00:00 2001 From: Adriaan Mulder Date: Mon, 17 Jul 2017 17:18:48 -0700 Subject: [PATCH 1/3] Basic graphql working with graphiql. --- app/__init__.py | 3 ++- app/libs/graphene/__init__.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 app/libs/graphene/__init__.py diff --git a/app/__init__.py b/app/__init__.py index d0c1894..3fdf04e 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -1,8 +1,8 @@ import importlib import logging - from flask import Flask, request +from app.libs.graphene import load as load_graphene import config @@ -14,6 +14,7 @@ def create_app(): load_extensions(app) load_models(app) load_blueprints(app) + load_graphene(app) return app diff --git a/app/libs/graphene/__init__.py b/app/libs/graphene/__init__.py new file mode 100644 index 0000000..d330823 --- /dev/null +++ b/app/libs/graphene/__init__.py @@ -0,0 +1,23 @@ +import graphene +from flask_graphql import GraphQLView + + +class Query(graphene.ObjectType): + hello = graphene.String() + + def resolve_hello(self, args, context, info): + return 'World' + + +schema = graphene.Schema(query=Query) + + +def load(app): + app.add_url_rule( + '/graphql', + view_func=GraphQLView.as_view( + 'graphql', + schema=schema, + graphiql=True + ) + ) From ed46d31e3771133c7f777055c5c03e90b6fbb716 Mon Sep 17 00:00:00 2001 From: Adriaan Mulder Date: Mon, 17 Jul 2017 17:22:00 -0700 Subject: [PATCH 2/3] Update requirements. --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/requirements.txt b/requirements.txt index 53e5429..46cd43a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,5 @@ flask-mongoengine flask-wtf gunicorn boto3 +Flask-GraphQL +graphene From a76dae04952d65db20f71f4301abe6abe26f85e8 Mon Sep 17 00:00:00 2001 From: Adriaan Mulder Date: Tue, 18 Jul 2017 10:25:51 -0700 Subject: [PATCH 3/3] Add graphene mutation and query example that uses mongo collection. --- app/libs/graphene/__init__.py | 14 ++++++++++++- app/libs/graphene/user.py | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/libs/graphene/user.py diff --git a/app/libs/graphene/__init__.py b/app/libs/graphene/__init__.py index d330823..397ef6d 100644 --- a/app/libs/graphene/__init__.py +++ b/app/libs/graphene/__init__.py @@ -1,15 +1,27 @@ 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) +schema = graphene.Schema(query=Query, mutation=Mutation) def load(app): diff --git a/app/libs/graphene/user.py b/app/libs/graphene/user.py new file mode 100644 index 0000000..5b8cc3a --- /dev/null +++ b/app/libs/graphene/user.py @@ -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)