2018-11-26 16:08:16 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from http import HTTPStatus
|
|
|
|
|
|
|
|
import graphene
|
|
|
|
|
2019-01-28 19:45:10 +03:00
|
|
|
import copy
|
|
|
|
|
2018-11-26 16:08:16 +03:00
|
|
|
from webserver import RequestHandler, WebServer, MkHandlers, Response
|
|
|
|
|
2019-01-28 15:38:38 +03:00
|
|
|
from enum import Enum
|
|
|
|
|
2019-08-23 11:57:19 +03:00
|
|
|
import time
|
|
|
|
|
2019-11-13 23:25:11 +03:00
|
|
|
HGE_URLS=[]
|
|
|
|
|
2018-11-26 16:08:16 +03:00
|
|
|
def mkJSONResp(graphql_result):
|
|
|
|
return Response(HTTPStatus.OK, graphql_result.to_dict(),
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
|
|
|
|
class HelloWorldHandler(RequestHandler):
|
|
|
|
def get(self, request):
|
|
|
|
return Response(HTTPStatus.OK, 'hello world')
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
|
|
|
|
class Hello(graphene.ObjectType):
|
|
|
|
hello = graphene.String(arg=graphene.String(default_value="world"))
|
|
|
|
|
2019-08-23 11:57:19 +03:00
|
|
|
delayedHello = graphene.String(arg=graphene.String(default_value="world"))
|
|
|
|
|
2018-11-26 16:08:16 +03:00
|
|
|
def resolve_hello(self, info, arg):
|
|
|
|
return "Hello " + arg
|
|
|
|
|
2019-08-23 11:57:19 +03:00
|
|
|
def resolve_delayedHello(self, info, arg):
|
|
|
|
time.sleep(10)
|
|
|
|
return "Hello " + arg
|
|
|
|
|
2018-12-12 15:01:18 +03:00
|
|
|
hello_schema = graphene.Schema(query=Hello, subscription=Hello)
|
2018-11-26 16:08:16 +03:00
|
|
|
|
|
|
|
class HelloGraphQL(RequestHandler):
|
|
|
|
def get(self, request):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = hello_schema.execute(request.json['query'])
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
|
|
|
class User(graphene.ObjectType):
|
|
|
|
id = graphene.Int()
|
|
|
|
username = graphene.String()
|
|
|
|
def __init__(self, id, username):
|
|
|
|
self.id = id
|
|
|
|
self.username = username
|
|
|
|
|
|
|
|
def resolve_id(self, info):
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
def resolve_username(self, info):
|
|
|
|
return self.username
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_by_id(_id):
|
|
|
|
xs = list(filter(lambda u: u.id == _id, all_users))
|
|
|
|
if not xs:
|
|
|
|
return None
|
|
|
|
return xs[0]
|
|
|
|
|
|
|
|
all_users = [
|
|
|
|
User(1, 'jane'),
|
|
|
|
User(2, 'john'),
|
|
|
|
User(3, 'joe'),
|
|
|
|
]
|
|
|
|
|
|
|
|
class CreateUser(graphene.Mutation):
|
|
|
|
class Arguments:
|
|
|
|
id = graphene.Int(required=True)
|
|
|
|
username = graphene.String(required=True)
|
|
|
|
|
|
|
|
ok = graphene.Boolean()
|
|
|
|
user = graphene.Field(lambda: User)
|
|
|
|
|
|
|
|
def mutate(self, info, id, username):
|
|
|
|
user = User(id, username)
|
|
|
|
all_users.append(user)
|
|
|
|
return CreateUser(ok=True, user=user)
|
|
|
|
|
|
|
|
class UserQuery(graphene.ObjectType):
|
|
|
|
user = graphene.Field(User, id=graphene.Int(required=True))
|
|
|
|
allUsers = graphene.List(User)
|
|
|
|
|
|
|
|
def resolve_user(self, info, id):
|
|
|
|
return User.get_by_id(id)
|
|
|
|
|
|
|
|
def resolve_allUsers(self, info):
|
|
|
|
return all_users
|
|
|
|
|
|
|
|
class UserMutation(graphene.ObjectType):
|
|
|
|
createUser = CreateUser.Field()
|
|
|
|
user_schema = graphene.Schema(query=UserQuery, mutation=UserMutation)
|
|
|
|
|
|
|
|
class UserGraphQL(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = user_schema.execute(req.json['query'])
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
2019-01-28 18:31:37 +03:00
|
|
|
class timestamptz(graphene.types.Scalar):
|
|
|
|
@staticmethod
|
|
|
|
def serialize(t):
|
|
|
|
return "2018-12-20"
|
|
|
|
@staticmethod
|
|
|
|
def parse_literal(s):
|
|
|
|
return "2018-12-20"
|
|
|
|
@staticmethod
|
|
|
|
def parse_value(s):
|
|
|
|
return "2018-12-20"
|
|
|
|
|
2018-11-26 16:08:16 +03:00
|
|
|
class Country(graphene.ObjectType):
|
|
|
|
name = graphene.String()
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def resolve_name(self, info):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class CountryQuery(graphene.ObjectType):
|
|
|
|
country = graphene.Field(Country)
|
|
|
|
|
|
|
|
def resolve_country(self, info):
|
|
|
|
return Country("India")
|
|
|
|
|
|
|
|
country_schema = graphene.Schema(query=CountryQuery)
|
|
|
|
|
|
|
|
class CountryGraphQL(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = country_schema.execute(req.json['query'])
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
2018-12-12 15:01:18 +03:00
|
|
|
|
|
|
|
class person(graphene.ObjectType):
|
|
|
|
id = graphene.Int(required=True)
|
|
|
|
name = graphene.String()
|
2019-01-28 18:31:37 +03:00
|
|
|
created = graphene.Field(timestamptz)
|
2018-12-12 15:01:18 +03:00
|
|
|
|
|
|
|
def resolve_id(self, info):
|
|
|
|
return 42
|
|
|
|
def resolve_name(self, info):
|
|
|
|
return 'Arthur Dent'
|
2019-01-28 18:31:37 +03:00
|
|
|
def resolve_created(self, info):
|
|
|
|
return '2018-12-20'
|
2018-12-12 15:01:18 +03:00
|
|
|
|
|
|
|
class PersonQuery(graphene.ObjectType):
|
|
|
|
person_ = graphene.Field(person)
|
|
|
|
|
|
|
|
def resolve_person_(self, info):
|
|
|
|
return person()
|
|
|
|
|
|
|
|
person_schema = graphene.Schema(query=PersonQuery)
|
|
|
|
|
|
|
|
class PersonGraphQL(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = person_schema.execute(req.json['query'])
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
2019-06-04 13:10:28 +03:00
|
|
|
# GraphQL server that returns Set-Cookie response header
|
|
|
|
class SampleAuth(graphene.ObjectType):
|
|
|
|
hello = graphene.String(arg=graphene.String(default_value="world"))
|
|
|
|
|
|
|
|
def resolve_hello(self, info, arg):
|
|
|
|
return "Hello " + arg
|
|
|
|
|
|
|
|
sample_auth_schema = graphene.Schema(query=SampleAuth,
|
|
|
|
subscription=SampleAuth)
|
|
|
|
|
|
|
|
class SampleAuthGraphQL(RequestHandler):
|
|
|
|
def get(self, request):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
2019-07-11 08:37:06 +03:00
|
|
|
res = sample_auth_schema.execute(request.json['query'])
|
2019-06-04 13:10:28 +03:00
|
|
|
resp = mkJSONResp(res)
|
|
|
|
resp.headers['Set-Cookie'] = 'abcd'
|
|
|
|
resp.headers['Custom-Header'] = 'custom-value'
|
|
|
|
return resp
|
|
|
|
|
|
|
|
# GraphQL server with interfaces
|
2019-01-28 19:45:10 +03:00
|
|
|
|
|
|
|
class Character(graphene.Interface):
|
|
|
|
id = graphene.ID(required=True)
|
|
|
|
name = graphene.String(required=True)
|
|
|
|
|
|
|
|
def __init__(self, id, name):
|
|
|
|
self.id = id
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
class Human(graphene.ObjectType):
|
|
|
|
class Meta:
|
|
|
|
interfaces = (Character, )
|
|
|
|
|
|
|
|
home_planet = graphene.String()
|
|
|
|
|
|
|
|
def __init__(self, home_planet, character):
|
|
|
|
self.home_planet = home_planet
|
|
|
|
self.character = character
|
|
|
|
|
|
|
|
def resolve_id(self, info):
|
|
|
|
return self.character.id
|
|
|
|
|
|
|
|
def resolve_name(self, info):
|
|
|
|
return self.character.name
|
|
|
|
|
|
|
|
def refolve_primary_function(self, info):
|
|
|
|
return self.home_planet
|
|
|
|
|
|
|
|
class Droid(graphene.ObjectType):
|
|
|
|
class Meta:
|
|
|
|
interfaces = (Character, )
|
|
|
|
|
|
|
|
primary_function = graphene.String()
|
|
|
|
|
|
|
|
def __init__(self, primary_function, character):
|
|
|
|
self.primary_function = primary_function
|
|
|
|
self.character = character
|
|
|
|
|
|
|
|
def resolve_id(self, info):
|
|
|
|
return self.character.id
|
|
|
|
|
|
|
|
def resolve_name(self, info):
|
|
|
|
return self.character.name
|
|
|
|
|
|
|
|
def resolve_primary_function(self, info):
|
|
|
|
return self.primary_function
|
|
|
|
|
|
|
|
class CharacterSearchResult(graphene.Union):
|
|
|
|
class Meta:
|
|
|
|
types = (Human,Droid)
|
|
|
|
|
|
|
|
all_characters = {
|
|
|
|
4: Droid("Astromech", Character(1,'R2-D2')),
|
|
|
|
5: Human("Tatooine", Character(2, "Luke Skywalker")),
|
|
|
|
}
|
|
|
|
|
|
|
|
character_search_results = {
|
|
|
|
1: Droid("Astromech", Character(6,'R2-D2')),
|
|
|
|
2: Human("Tatooine", Character(7, "Luke Skywalker")),
|
|
|
|
}
|
|
|
|
|
|
|
|
class CharacterIFaceQuery(graphene.ObjectType):
|
|
|
|
hero = graphene.Field(
|
|
|
|
Character,
|
|
|
|
required=False,
|
|
|
|
episode=graphene.Int(required=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
def resolve_hero(_, info, episode):
|
|
|
|
return all_characters.get(episode)
|
|
|
|
|
|
|
|
schema = graphene.Schema(query=CharacterIFaceQuery, types=[Human, Droid])
|
|
|
|
|
|
|
|
character_interface_schema = graphene.Schema(query=CharacterIFaceQuery, types=[Human, Droid])
|
|
|
|
|
|
|
|
class CharacterInterfaceGraphQL(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
|
|
|
class InterfaceGraphQLErrEmptyFieldList(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'INTERFACE':
|
|
|
|
t['fields'] = []
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class InterfaceGraphQLErrUnknownInterface(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'OBJECT' and t['name'] == 'Droid':
|
|
|
|
t['interfaces'][0]['name'] = 'UnknownIFace'
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class InterfaceGraphQLErrWrongFieldType(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
#Remove id field from Droid
|
|
|
|
if t['kind'] == 'OBJECT' and t['name'] == 'Droid':
|
|
|
|
for f in t['fields'].copy():
|
|
|
|
if f['name'] == 'id':
|
|
|
|
f['type']['ofType']['name'] = 'String'
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class InterfaceGraphQLErrMissingField(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
#Remove id field from Droid
|
|
|
|
if t['kind'] == 'OBJECT' and t['name'] == 'Droid':
|
|
|
|
for f in t['fields'].copy():
|
|
|
|
if f['name'] == 'id':
|
|
|
|
t['fields'].remove(f)
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
ifaceArg = {
|
|
|
|
"name": "ifaceArg",
|
|
|
|
"description": None,
|
|
|
|
"type": {
|
|
|
|
"kind": "NON_NULL",
|
|
|
|
"name": None,
|
|
|
|
"ofType": {
|
|
|
|
"kind": "SCALAR",
|
|
|
|
"name": "Int",
|
|
|
|
"ofType": None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"defaultValue": None
|
|
|
|
}
|
|
|
|
|
|
|
|
class InterfaceGraphQLErrMissingArg(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'INTERFACE':
|
|
|
|
for f in t['fields']:
|
|
|
|
if f['name'] == 'id':
|
|
|
|
f['args'].append(ifaceArg)
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class InterfaceGraphQLErrWrongArgType(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
objArg = copy.deepcopy(ifaceArg)
|
|
|
|
objArg['type']['ofType']['name'] = 'String'
|
|
|
|
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in filter(lambda ty : ty['kind'] == 'INTERFACE', typesList):
|
|
|
|
for f in filter(lambda fld: fld['name'] == 'id', t['fields']):
|
|
|
|
f['args'].append(ifaceArg)
|
|
|
|
|
|
|
|
for t in filter(lambda ty: ty['name'] in ['Droid','Human'], typesList):
|
|
|
|
for f in filter(lambda fld: fld['name'] == 'id', t['fields']):
|
|
|
|
f['args'].append(ifaceArg if t['name'] == 'Droid' else objArg)
|
|
|
|
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class InterfaceGraphQLErrExtraNonNullArg(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = character_interface_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'OBJECT' and t['name'] == 'Droid':
|
|
|
|
for f in t['fields']:
|
|
|
|
if f['name'] == 'id':
|
|
|
|
f['args'].append({
|
|
|
|
"name": "extraArg",
|
|
|
|
"description": None,
|
|
|
|
"type": {
|
|
|
|
"kind": "NON_NULL",
|
|
|
|
"name": None,
|
|
|
|
"ofType": {
|
|
|
|
"kind": "SCALAR",
|
|
|
|
"name": "Int",
|
|
|
|
"ofType": None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"defaultValue": None
|
|
|
|
})
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
#GraphQL server involving union type
|
|
|
|
|
|
|
|
class UnionQuery(graphene.ObjectType):
|
|
|
|
search = graphene.Field(
|
|
|
|
CharacterSearchResult,
|
|
|
|
required=False,
|
|
|
|
episode=graphene.Int(required=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
def resolve_search(_, info, episode):
|
|
|
|
return character_search_results.get(episode)
|
|
|
|
|
|
|
|
union_schema = graphene.Schema(query=UnionQuery, types=[Human, Droid])
|
|
|
|
|
|
|
|
class UnionGraphQL(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = union_schema.execute(req.json['query'])
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
|
|
|
class UnionGraphQLSchemaErrUnknownTypes(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = union_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'UNION':
|
|
|
|
for i, p in enumerate(t['possibleTypes']):
|
|
|
|
p['name'] = 'Unknown' + str(i)
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class UnionGraphQLSchemaErrSubTypeInterface(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = union_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'UNION':
|
|
|
|
for p in t['possibleTypes']:
|
|
|
|
p['name'] = 'Character'
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class UnionGraphQLSchemaErrNoMemberTypes(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = union_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'UNION':
|
|
|
|
t['possibleTypes'] = []
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
class UnionGraphQLSchemaErrWrappedType(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = union_schema.execute(req.json['query'])
|
|
|
|
respDict = res.to_dict()
|
|
|
|
typesList = respDict.get('data',{}).get('__schema',{}).get('types',None)
|
|
|
|
if typesList is not None:
|
|
|
|
for t in typesList:
|
|
|
|
if t['kind'] == 'UNION':
|
|
|
|
for i, p in enumerate(t['possibleTypes']):
|
|
|
|
t['possibleTypes'][i] = {
|
|
|
|
"kind": "NON_NULL",
|
|
|
|
"name": None,
|
|
|
|
"ofType": p
|
|
|
|
}
|
|
|
|
return Response(HTTPStatus.OK, respDict,
|
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
|
|
|
#GraphQL server with default values for inputTypes
|
|
|
|
|
2019-01-28 15:38:38 +03:00
|
|
|
class InpObjType(graphene.InputObjectType):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def default(cls):
|
|
|
|
meta = cls._meta
|
|
|
|
fields = meta.fields
|
|
|
|
default_fields = {name: field.default_value for name, field in fields.items()}
|
|
|
|
container = meta.container
|
|
|
|
return container(**default_fields)
|
|
|
|
|
|
|
|
class SizeObj(graphene.ObjectType):
|
|
|
|
width = graphene.Int()
|
|
|
|
height = graphene.Float()
|
|
|
|
shape = graphene.String()
|
|
|
|
hasTag = graphene.Boolean()
|
|
|
|
class Color(Enum):
|
|
|
|
RED = 1
|
|
|
|
GREEN = 2
|
|
|
|
BLUE = 3
|
|
|
|
|
|
|
|
GQColorEnum = graphene.Enum.from_enum(Color)
|
|
|
|
|
|
|
|
class SizeInput(InpObjType):
|
|
|
|
width = graphene.Int(default_value=100)
|
|
|
|
height = graphene.Float(default_value=100.1)
|
|
|
|
shape = graphene.String(default_value="cube")
|
|
|
|
hasTag = graphene.Boolean(default_value=False)
|
|
|
|
|
|
|
|
def asSizeObj(self):
|
|
|
|
return SizeObj(width=self.width, height=self.height, shape=self.shape, hasTag=self.hasTag)
|
|
|
|
|
|
|
|
|
|
|
|
class Echo(graphene.ObjectType):
|
|
|
|
intFld = graphene.Int()
|
|
|
|
listFld = graphene.List(graphene.String)
|
|
|
|
objFld = graphene.Field(SizeObj)
|
|
|
|
enumFld = graphene.Field(GQColorEnum)
|
|
|
|
|
|
|
|
class EchoQuery(graphene.ObjectType):
|
|
|
|
echo = graphene.Field(
|
|
|
|
Echo,
|
|
|
|
int_input=graphene.Int( default_value=1234),
|
|
|
|
list_input=graphene.Argument(graphene.List(graphene.String), default_value=["hi","there"]),
|
|
|
|
obj_input=graphene.Argument(SizeInput, default_value=SizeInput.default()),
|
|
|
|
enum_input=graphene.Argument(GQColorEnum, default_value=GQColorEnum.RED.name),
|
|
|
|
)
|
|
|
|
|
|
|
|
def resolve_echo(self, info, int_input, list_input, obj_input, enum_input):
|
|
|
|
#print (int_input, list_input, obj_input)
|
|
|
|
return Echo(intFld=int_input, listFld=list_input, objFld=obj_input, enumFld=enum_input)
|
|
|
|
|
|
|
|
echo_schema = graphene.Schema(query=EchoQuery)
|
|
|
|
|
|
|
|
class EchoGraphQL(RequestHandler):
|
|
|
|
def get(self, req):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
def post(self, req):
|
|
|
|
if not req.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = echo_schema.execute(req.json['query'])
|
2019-06-04 13:10:28 +03:00
|
|
|
resp_dict = res.to_dict()
|
|
|
|
types_list = resp_dict.get('data',{}).get('__schema',{}).get('types', None)
|
2019-01-28 19:45:10 +03:00
|
|
|
#Hack around enum default_value serialization issue: https://github.com/graphql-python/graphql-core/issues/166
|
2019-06-04 13:10:28 +03:00
|
|
|
if types_list is not None:
|
|
|
|
for t in filter(lambda ty: ty['name'] == 'EchoQuery', types_list):
|
2019-01-28 15:38:38 +03:00
|
|
|
for f in filter(lambda fld: fld['name'] == 'echo', t['fields']):
|
|
|
|
for a in filter(lambda arg: arg['name'] == 'enumInput', f['args']):
|
|
|
|
a['defaultValue'] = 'RED'
|
2019-06-04 13:10:28 +03:00
|
|
|
return Response(HTTPStatus.OK, resp_dict,
|
2019-01-28 15:38:38 +03:00
|
|
|
{'Content-Type': 'application/json'})
|
|
|
|
|
2019-05-06 14:27:34 +03:00
|
|
|
|
|
|
|
class HeaderTest(graphene.ObjectType):
|
|
|
|
wassup = graphene.String(arg=graphene.String(default_value='world'))
|
|
|
|
|
|
|
|
def resolve_wassup(self, info, arg):
|
|
|
|
headers = info.context
|
2019-11-13 23:25:11 +03:00
|
|
|
hosts = list(map(lambda o: urlparse(o).netloc, HGE_URLS))
|
2019-05-06 14:27:34 +03:00
|
|
|
if not (headers.get_all('x-hasura-test') == ['abcd'] and
|
|
|
|
headers.get_all('x-hasura-role') == ['user'] and
|
|
|
|
headers.get_all('x-hasura-user-id') == ['abcd1234'] and
|
2019-05-13 15:47:01 +03:00
|
|
|
headers.get_all('content-type') == ['application/json'] and
|
2019-11-13 23:25:11 +03:00
|
|
|
headers.get_all('Authorization') == ['Bearer abcdef'] and
|
|
|
|
len(headers.get_all('x-forwarded-host')) == 1 and
|
|
|
|
all(host in headers.get_all('x-forwarded-host') for host in hosts) and
|
|
|
|
headers.get_all('x-forwarded-user-agent') == ['python-requests/2.22.0']):
|
|
|
|
raise Exception('headers dont match. Received: ' + str(headers))
|
2019-05-06 14:27:34 +03:00
|
|
|
|
|
|
|
return "Hello " + arg
|
|
|
|
|
|
|
|
header_test_schema = graphene.Schema(query=HeaderTest)
|
|
|
|
|
|
|
|
class HeaderTestGraphQL(RequestHandler):
|
|
|
|
def get(self, request):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = header_test_schema.execute(request.json['query'],
|
|
|
|
context=request.headers)
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
|
|
|
|
class Message(graphene.ObjectType):
|
|
|
|
id = graphene.Int()
|
|
|
|
msg = graphene.String()
|
|
|
|
def __init__(self, id, msg):
|
|
|
|
self.id = id
|
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
def resolve_id(self, info):
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
def resolve_msg(self, info):
|
|
|
|
return self.msg
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_by_id(_id):
|
|
|
|
xs = list(filter(lambda u: u.id == _id, all_messages))
|
|
|
|
if not xs:
|
|
|
|
return None
|
|
|
|
return xs[0]
|
|
|
|
|
|
|
|
all_messages = [
|
|
|
|
Message(1, 'You win!'),
|
|
|
|
Message(2, 'You lose!')
|
|
|
|
]
|
|
|
|
|
|
|
|
class MessagesQuery(graphene.ObjectType):
|
|
|
|
message = graphene.Field(Message, id=graphene.Int(required=True))
|
|
|
|
messages = graphene.List(Message)
|
|
|
|
|
|
|
|
def resolve_message(self, info, id):
|
|
|
|
return Message.get_by_id(id)
|
|
|
|
|
|
|
|
def resolve_messages(self, info):
|
|
|
|
return all_messages
|
|
|
|
|
|
|
|
messages_schema = graphene.Schema(query=MessagesQuery)
|
|
|
|
|
|
|
|
class MessagesGraphQL(RequestHandler):
|
|
|
|
def get(self, request):
|
|
|
|
return Response(HTTPStatus.METHOD_NOT_ALLOWED)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
if not request.json:
|
|
|
|
return Response(HTTPStatus.BAD_REQUEST)
|
|
|
|
res = messages_schema.execute(request.json['query'])
|
|
|
|
return mkJSONResp(res)
|
|
|
|
|
2018-11-26 16:08:16 +03:00
|
|
|
handlers = MkHandlers({
|
|
|
|
'/hello': HelloWorldHandler,
|
|
|
|
'/hello-graphql': HelloGraphQL,
|
|
|
|
'/user-graphql': UserGraphQL,
|
|
|
|
'/country-graphql': CountryGraphQL,
|
2019-01-28 19:45:10 +03:00
|
|
|
'/character-iface-graphql' : CharacterInterfaceGraphQL,
|
|
|
|
'/iface-graphql-err-empty-field-list' : InterfaceGraphQLErrEmptyFieldList,
|
|
|
|
'/iface-graphql-err-unknown-iface' : InterfaceGraphQLErrUnknownInterface,
|
|
|
|
'/iface-graphql-err-missing-field' : InterfaceGraphQLErrMissingField,
|
|
|
|
'/iface-graphql-err-wrong-field-type' : InterfaceGraphQLErrWrongFieldType,
|
|
|
|
'/iface-graphql-err-missing-arg' : InterfaceGraphQLErrMissingArg,
|
|
|
|
'/iface-graphql-err-wrong-arg-type' : InterfaceGraphQLErrWrongArgType,
|
|
|
|
'/iface-graphql-err-extra-non-null-arg' : InterfaceGraphQLErrExtraNonNullArg,
|
|
|
|
'/union-graphql' : UnionGraphQL,
|
|
|
|
'/union-graphql-err-unknown-types' : UnionGraphQLSchemaErrUnknownTypes,
|
|
|
|
'/union-graphql-err-subtype-iface' : UnionGraphQLSchemaErrSubTypeInterface,
|
|
|
|
'/union-graphql-err-no-member-types' : UnionGraphQLSchemaErrNoMemberTypes,
|
|
|
|
'/union-graphql-err-wrapped-type' : UnionGraphQLSchemaErrWrappedType,
|
2019-01-28 15:38:38 +03:00
|
|
|
'/default-value-echo-graphql' : EchoGraphQL,
|
2019-05-06 14:27:34 +03:00
|
|
|
'/person-graphql': PersonGraphQL,
|
2019-06-04 13:10:28 +03:00
|
|
|
'/header-graphql': HeaderTestGraphQL,
|
2020-05-27 18:02:58 +03:00
|
|
|
'/messages-graphql' : MessagesGraphQL,
|
|
|
|
'/auth-graphql': SampleAuthGraphQL
|
2018-11-26 16:08:16 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def create_server(host='127.0.0.1', port=5000):
|
|
|
|
return WebServer((host, port), handlers)
|
|
|
|
|
|
|
|
def stop_server(server):
|
|
|
|
server.shutdown()
|
|
|
|
server.server_close()
|
|
|
|
|
2019-11-13 23:25:11 +03:00
|
|
|
def set_hge_urls(hge_urls = []):
|
|
|
|
global HGE_URLS
|
|
|
|
HGE_URLS=hge_urls
|
|
|
|
|
2018-11-26 16:08:16 +03:00
|
|
|
if __name__ == '__main__':
|
2019-01-28 16:55:28 +03:00
|
|
|
s = create_server(host='0.0.0.0')
|
2018-11-26 16:08:16 +03:00
|
|
|
s.serve_forever()
|