From ff7bf20f6d4eb2cd3d148598b48e8f8151f463cf Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 4 Jun 2016 18:48:32 -0700 Subject: [PATCH] Added some mutable comments --- graphene/types/schema.py | 31 +++++++++++++++-- graphene/types/tests/test_objecttype.py | 46 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/graphene/types/schema.py b/graphene/types/schema.py index f1c04a28..59fd1fb8 100644 --- a/graphene/types/schema.py +++ b/graphene/types/schema.py @@ -1,8 +1,10 @@ from graphql import graphql, GraphQLSchema from graphql.utils.introspection_query import introspection_query from graphql.utils.schema_printer import print_schema +# from graphql.type.schema import assert_object_implements_interface from ..utils.get_graphql_type import get_graphql_type +# from collections import defaultdict class Schema(GraphQLSchema): @@ -15,17 +17,24 @@ class Schema(GraphQLSchema): mutation = get_graphql_type(mutation) if subscription: subscription = get_graphql_type(subscription) - if types: - types = map(get_graphql_type, types) + self.types = types self._executor = executor super(Schema, self).__init__( query=query, mutation=mutation, subscription=subscription, directives=directives, - types=types + types=self.types ) + @property + def types(self): + return map(get_graphql_type, self._types or []) + + @types.setter + def types(self, value): + self._types = value + def execute(self, request_string='', root_value=None, variable_values=None, context_value=None, operation_name=None, executor=None): return graphql( @@ -52,3 +61,19 @@ class Schema(GraphQLSchema): def lazy(self, _type): return lambda: self.get_type(_type) + + # 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) diff --git a/graphene/types/tests/test_objecttype.py b/graphene/types/tests/test_objecttype.py index 374546b5..da22223c 100644 --- a/graphene/types/tests/test_objecttype.py +++ b/graphene/types/tests/test_objecttype.py @@ -2,8 +2,10 @@ import pytest from graphql import GraphQLObjectType, GraphQLField, GraphQLString, GraphQLInterfaceType +from ..schema import Schema from ..objecttype import ObjectType from ..interface import Interface +from ..scalars import String from ..field import Field @@ -161,3 +163,47 @@ def test_objecttype_graphene_interface(): assert graphql_type.is_type_of(GrapheneObjectType(), None, None) fields = graphql_type.get_fields() assert 'field' in fields + + +# def test_objecttype_graphene_interface_extended(): +# class GrapheneInterface(Interface): +# field = Field(GraphQLString) + +# class GrapheneObjectType(ObjectType): +# class Meta: +# interfaces = [GrapheneInterface] + +# schema = Schema(query=GrapheneObjectType) +# assert str(schema) == """ +# schema { +# query: GrapheneObjectType +# } + +# interface GrapheneInterface { +# field: String +# } + +# type GrapheneObjectType implements GrapheneInterface { +# field: String +# } +# """.lstrip() +# GrapheneInterface._meta.graphql_type.add_field(Field(String, name='dynamic')) +# # GrapheneObjectType._meta.graphql_type._field_map = None +# assert GrapheneInterface._meta.graphql_type.get_fields().keys() == ['field', 'dynamic'] +# assert GrapheneObjectType._meta.graphql_type.get_fields().keys() == ['field', 'dynamic'] +# schema.rebuild() +# assert str(schema) == """ +# schema { +# query: GrapheneObjectType +# } + +# interface GrapheneInterface { +# field: String +# dynamic: String +# } + +# type GrapheneObjectType implements GrapheneInterface { +# field: String +# dynamic: String +# } +# """.lstrip()