From 855cee2f346dbc6a530ea166c81bfdd7d03e3e9f Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 3 Oct 2015 02:42:56 -0700 Subject: [PATCH] Added custom GraphQLSchema --- graphene/contrib/django/fields.py | 11 +++++++---- graphene/core/schema.py | 10 ++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/graphene/contrib/django/fields.py b/graphene/contrib/django/fields.py index 292e6333..4d70b9b6 100644 --- a/graphene/contrib/django/fields.py +++ b/graphene/contrib/django/fields.py @@ -12,6 +12,7 @@ from django.db.models.query import QuerySet from django.db.models.manager import Manager +@memoize def get_type_for_model(schema, model): schema = schema types = schema.types.values() @@ -32,13 +33,15 @@ def lazy_map(value, func): class DjangoConnectionField(relay.ConnectionField): def wrap_resolved(self, value, instance, args, info): - return lazy_map(value, self.field_type) + schema = info.schema.graphene_schema + return lazy_map(value, self.get_object_type(schema)) class LazyListField(ListField): - def resolve(self, value, instance, args, info): - resolved = super(LazyListField, self).resolve(value, instance, args, info) - return lazy_map(resolved, self.field_type) + def resolve(self, instance, args, info): + schema = info.schema.graphene_schema + resolved = super(LazyListField, self).resolve(instance, args, info) + return lazy_map(resolved, self.get_object_type(schema)) class ConnectionOrListField(LazyField): diff --git a/graphene/core/schema.py b/graphene/core/schema.py index da26e7d0..6d979f76 100644 --- a/graphene/core/schema.py +++ b/graphene/core/schema.py @@ -2,12 +2,18 @@ from functools import wraps from graphql.core import graphql from graphql.core.type import ( - GraphQLSchema + GraphQLSchema as _GraphQLSchema ) from graphene import signals from graphene.utils import cached_property +class GraphQLSchema(_GraphQLSchema): + def __init__(self, schema, *args, **kwargs): + self.graphene_schema = schema + super(GraphQLSchema, self).__init__(*args, **kwargs) + + class Schema(object): _query = None @@ -34,7 +40,7 @@ class Schema(object): def schema(self): if not self._query_type: raise Exception('You have to define a base query type') - return GraphQLSchema(query=self._query_type, mutation=self.mutation) + return GraphQLSchema(self, query=self._query_type, mutation=self.mutation) def associate_internal_type(self, internal_type, object_type): self._internal_types[internal_type.name] = object_type