mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Simplified schema code
This commit is contained in:
parent
b6df2d881d
commit
a7f3b77eae
|
@ -1,5 +1,4 @@
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from functools import wraps
|
|
||||||
|
|
||||||
from graphene import signals
|
from graphene import signals
|
||||||
from graphql.core.execution.executor import Executor
|
from graphql.core.execution.executor import Executor
|
||||||
|
@ -21,7 +20,7 @@ class Schema(object):
|
||||||
_executor = None
|
_executor = None
|
||||||
|
|
||||||
def __init__(self, query=None, mutation=None, name='Schema', executor=None):
|
def __init__(self, query=None, mutation=None, name='Schema', executor=None):
|
||||||
self._internal_types = {}
|
self._types_names = {}
|
||||||
self._types = {}
|
self._types = {}
|
||||||
self.mutation = mutation
|
self.mutation = mutation
|
||||||
self.query = query
|
self.query = query
|
||||||
|
@ -36,7 +35,9 @@ class Schema(object):
|
||||||
if not object_type:
|
if not object_type:
|
||||||
return
|
return
|
||||||
if object_type not in self._types:
|
if object_type not in self._types:
|
||||||
self._types[object_type] = object_type.internal_type(self)
|
internal_type = object_type.internal_type(self)
|
||||||
|
self._types[object_type] = internal_type
|
||||||
|
self._types_names[internal_type.name] = object_type
|
||||||
return self._types[object_type]
|
return self._types[object_type]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -72,22 +73,19 @@ class Schema(object):
|
||||||
raise Exception('You have to define a base query type')
|
raise Exception('You have to define a base query type')
|
||||||
return GraphQLSchema(self, query=self.T(self._query), mutation=self.T(self._mutation))
|
return GraphQLSchema(self, query=self.T(self._query), mutation=self.T(self._mutation))
|
||||||
|
|
||||||
def associate_internal_type(self, internal_type, object_type):
|
|
||||||
self._internal_types[internal_type.name] = object_type
|
|
||||||
|
|
||||||
def register(self, object_type):
|
def register(self, object_type):
|
||||||
self._internal_types[object_type._meta.type_name] = object_type
|
self._types_names[object_type._meta.type_name] = object_type
|
||||||
return object_type
|
return object_type
|
||||||
|
|
||||||
def get_type(self, type_name):
|
def get_type(self, type_name):
|
||||||
self.schema._build_type_map()
|
self.schema._build_type_map()
|
||||||
if type_name not in self._internal_types:
|
if type_name not in self._types_names:
|
||||||
raise Exception('Type %s not found in %r' % (type_name, self))
|
raise Exception('Type %s not found in %r' % (type_name, self))
|
||||||
return self._internal_types[type_name]
|
return self._types_names[type_name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def types(self):
|
def types(self):
|
||||||
return self._internal_types
|
return self._types_names
|
||||||
|
|
||||||
def execute(self, request='', root=None, vars=None, operation_name=None, **kwargs):
|
def execute(self, request='', root=None, vars=None, operation_name=None, **kwargs):
|
||||||
root = root or object()
|
root = root or object()
|
||||||
|
@ -102,14 +100,3 @@ class Schema(object):
|
||||||
|
|
||||||
def introspect(self):
|
def introspect(self):
|
||||||
return self.execute(introspection_query).data
|
return self.execute(introspection_query).data
|
||||||
|
|
||||||
|
|
||||||
def register_internal_type(fun):
|
|
||||||
@wraps(fun)
|
|
||||||
def wrapper(cls, schema):
|
|
||||||
internal_type = fun(cls, schema)
|
|
||||||
if isinstance(schema, Schema):
|
|
||||||
schema.associate_internal_type(internal_type, cls)
|
|
||||||
return internal_type
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import six
|
||||||
|
|
||||||
from graphene import signals
|
from graphene import signals
|
||||||
from graphene.core.options import Options
|
from graphene.core.options import Options
|
||||||
from graphene.core.schema import register_internal_type
|
|
||||||
from graphql.core.type import (GraphQLArgument, GraphQLInputObjectType,
|
from graphql.core.type import (GraphQLArgument, GraphQLInputObjectType,
|
||||||
GraphQLInterfaceType, GraphQLObjectType)
|
GraphQLInterfaceType, GraphQLObjectType)
|
||||||
|
|
||||||
|
@ -185,7 +184,6 @@ class BaseObjectType(object):
|
||||||
return schema.T(objecttype)
|
return schema.T(objecttype)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@register_internal_type
|
|
||||||
def internal_type(cls, schema):
|
def internal_type(cls, schema):
|
||||||
fields = lambda: OrderedDict([(f.name, f.internal_field(schema))
|
fields = lambda: OrderedDict([(f.name, f.internal_field(schema))
|
||||||
for f in cls._meta.fields])
|
for f in cls._meta.fields])
|
||||||
|
@ -223,7 +221,6 @@ class Mutation(six.with_metaclass(ObjectTypeMeta, BaseObjectType)):
|
||||||
class InputObjectType(ObjectType):
|
class InputObjectType(ObjectType):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@register_internal_type
|
|
||||||
def internal_type(cls, schema):
|
def internal_type(cls, schema):
|
||||||
fields = lambda: OrderedDict([(f.name, f.internal_field(schema))
|
fields = lambda: OrderedDict([(f.name, f.internal_field(schema))
|
||||||
for f in cls._meta.fields])
|
for f in cls._meta.fields])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user