Added schema

This commit is contained in:
Syrus Akbary 2016-08-11 00:27:37 -07:00
parent 453d6d6ab7
commit ac0e699069
4 changed files with 159 additions and 2 deletions

View File

@ -23,8 +23,9 @@ class AbstractTypeMeta(type):
fields = get_fields_in_type(AbstractType, attrs)
yank_fields_from_attrs(attrs, fields)
options = attrs.get('_meta', Options())
options.fields = fields
options = Options(
fields=fields
)
cls = type.__new__(cls, name, bases, dict(attrs, _meta=options))
return cls

View File

@ -31,6 +31,10 @@ class Scalar(six.with_metaclass(ScalarTypeMeta, UnmountedType)):
parse_value = None
parse_literal = None
@classmethod
def get_type(cls):
return cls
# As per the GraphQL Spec, Integers are only treated as valid when a valid
# 32-bit signed integer, providing the broadest support across platforms.
#

View File

@ -0,0 +1,82 @@
import inspect
from graphql import GraphQLSchema, graphql
from graphql.utils.introspection_query import introspection_query
from graphql.utils.schema_printer import print_schema
from .objecttype import ObjectType
# from ..utils.get_graphql_type import get_graphql_type
# from graphql.type.schema import assert_object_implements_interface
# from collections import defaultdict
class Schema(GraphQLSchema):
def __init__(self, query=None, mutation=None, subscription=None, directives=None, types=None, executor=None):
self._query = query
self._mutation = mutation
self._subscription = subscription
self.types = types
self._executor = executor
# super(Schema, self).__init__(
# query=query,
# mutation=mutation,
# subscription=subscription,
# directives=directives,
# types=self.types
# )
def execute(self, request_string='', root_value=None, variable_values=None,
context_value=None, operation_name=None, executor=None):
return graphql(
schema=self,
request_string=request_string,
root_value=root_value,
context_value=context_value,
variable_values=variable_values,
operation_name=operation_name,
executor=executor or self._executor
)
def register(self, object_type):
self.types.append(object_type)
def introspect(self):
return self.execute(introspection_query).data
def __str__(self):
return print_schema(self)
def lazy(self, _type):
return lambda: self.get_type(_type)
def _type_map_reducer(self, map, type):
if not type:
return map
if inspect.isclass(type) and issubclass(type, (ObjectType)):
return self._type_map_reducer_graphene(map, type)
return super(Schema, self)._type_map_reducer(map, type)
def _type_map_reducer_graphene(self, map, type):
# from .structures import List, NonNull
return map
# def rebuild(self):
# self._possible_type_map = defaultdict(set)
# self._type_map = self._build_type_map(self.types)
# # Keep track of all implementations by interface name.
# self._implementations = defaultdict(list)
# for type in self._type_map.values():
# if isinstance(type, GraphQLObjectType):
# for interface in type.get_interfaces():
# self._implementations[interface.name].append(type)
# # Enforce correct interface implementations.
# for type in self._type_map.values():
# if isinstance(type, GraphQLObjectType):
# for interface in type.get_interfaces():
# assert_object_implements_interface(self, type, interface)

View File

@ -0,0 +1,70 @@
from collections import OrderedDict
from py.test import raises
from ..objecttype import ObjectType
from ..scalars import String, Int, Boolean
from ..field import Field
from ..structures import List
from ..schema import Schema
class Image(ObjectType):
url = String()
width = Int()
height = Int()
class Author(ObjectType):
id = String()
name = String()
pic = Field(Image) # width=Int(), height=Int()
recent_article = Field(lambda: Article)
class Article(ObjectType):
id = String()
is_published = Boolean()
author = Field(Author)
title = String()
body = String()
class Query(ObjectType):
article = Field(Article) # id=String()
feed = List(Article)
class Mutation(ObjectType):
write_article = Field(Article)
class Subscription(ObjectType):
article_subscribe = Field(Article) # id=String()
def test_defines_a_query_only_schema():
blog_schema = Schema(Query)
assert blog_schema.get_query_type() == Query
article_field = Query._meta.fields['article']
assert article_field.type == Article
assert article_field.type._meta.name == 'Article'
article_field_type = article_field.type
assert issubclass(article_field_type, ObjectType)
title_field = article_field_type._meta.fields['title']
assert title_field.type == String
author_field = article_field_type._meta.fields['author']
author_field_type = author_field.type
assert issubclass(author_field_type, ObjectType)
recent_article_field = author_field_type._meta.fields['recent_article']
assert recent_article_field.type() == Article
feed_field = Query._meta.fields['feed']
assert feed_field.type.of_type == Article