mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-26 11:33:55 +03:00
Improved schema
This commit is contained in:
parent
c4fba3b7ca
commit
3e77f258b4
|
@ -6,7 +6,8 @@ from graphql.utils.schema_printer import print_schema
|
|||
|
||||
|
||||
from .objecttype import ObjectType
|
||||
from .scalars import Scalar
|
||||
from .structures import List, NonNull
|
||||
from .scalars import Scalar, String
|
||||
# from ..utils.get_graphql_type import get_graphql_type
|
||||
|
||||
|
||||
|
@ -19,13 +20,14 @@ from collections import Iterable, OrderedDict, defaultdict
|
|||
from functools import reduce
|
||||
|
||||
from graphql.utils.type_comparators import is_equal_type, is_type_sub_type_of
|
||||
from graphql.type.definition import (GraphQLInputObjectType, GraphQLInterfaceType, GraphQLField,
|
||||
from graphql.type.definition import (GraphQLInputObjectType, GraphQLInterfaceType, GraphQLField,GraphQLScalarType,
|
||||
GraphQLList, GraphQLNonNull, GraphQLObjectType,
|
||||
GraphQLUnionType)
|
||||
from graphql.type.directives import (GraphQLDirective, GraphQLIncludeDirective,
|
||||
GraphQLSkipDirective)
|
||||
from graphql.type.introspection import IntrospectionSchema
|
||||
from graphql.type.schema import assert_object_implements_interface
|
||||
from graphql.type.scalars import GraphQLString
|
||||
|
||||
|
||||
class Schema(GraphQLSchema):
|
||||
|
@ -97,18 +99,35 @@ class Schema(GraphQLSchema):
|
|||
def _type_map_reducer(self, map, type):
|
||||
if not type:
|
||||
return map
|
||||
if inspect.isclass(type) and issubclass(type, (ObjectType, Scalar)):
|
||||
if isinstance(type, List) or (inspect.isclass(type) and issubclass(type, (ObjectType, Scalar))):
|
||||
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
|
||||
from ..generators.definitions import GrapheneObjectType
|
||||
from ..generators.definitions import GrapheneObjectType, GrapheneScalarType
|
||||
if isinstance(type, List):
|
||||
return self._type_map_reducer(map, type.of_type)
|
||||
if issubclass(type, String):
|
||||
map[type._meta.name] = GraphQLString
|
||||
return map
|
||||
|
||||
if type._meta.name in map:
|
||||
assert map[type._meta.name].graphene_type == type
|
||||
return map
|
||||
if issubclass(type, ObjectType):
|
||||
fields = OrderedDict()
|
||||
map[type._meta.name] = GrapheneObjectType(
|
||||
graphene_type=type,
|
||||
name=type._meta.name,
|
||||
description=type._meta.description,
|
||||
fields={},
|
||||
is_type_of=type.is_type_of,
|
||||
interfaces=type._meta.interfaces
|
||||
)
|
||||
for name, field in type._meta.fields.items():
|
||||
map = self._type_map_reducer(map, field.type)
|
||||
field_type = map.get(field.type._meta.name)
|
||||
field_type = self.get_field_type(map, field.type)
|
||||
_field = GraphQLField(
|
||||
field_type,
|
||||
args=field.args,
|
||||
|
@ -117,16 +136,22 @@ class Schema(GraphQLSchema):
|
|||
description=field.description
|
||||
)
|
||||
fields[name] = _field
|
||||
map[type._meta.name] = GrapheneObjectType(
|
||||
graphene_type=type,
|
||||
name=type._meta.name,
|
||||
description=type._meta.description,
|
||||
fields=fields,
|
||||
is_type_of=type.is_type_of,
|
||||
interfaces=type._meta.interfaces
|
||||
)
|
||||
map[type._meta.name].fields = fields
|
||||
# map[type._meta.name] = GrapheneScalarType(
|
||||
# graphene_type=type,
|
||||
# name=type._meta.name,
|
||||
# description=type._meta.description,
|
||||
|
||||
# serialize=getattr(type, 'serialize', None),
|
||||
# parse_value=getattr(type, 'parse_value', None),
|
||||
# parse_literal=getattr(type, 'parse_literal', None),
|
||||
# )
|
||||
return map
|
||||
|
||||
def get_field_type(self, map, type):
|
||||
if isinstance(type, List):
|
||||
return GraphQLList(self.get_field_type(map, type.of_type))
|
||||
return map.get(type._meta.name)
|
||||
# def rebuild(self):
|
||||
# self._possible_type_map = defaultdict(set)
|
||||
# self._type_map = self._build_type_map(self.types)
|
||||
|
|
|
@ -64,7 +64,7 @@ def test_defines_a_query_only_schema():
|
|||
assert issubclass(author_field_type, ObjectType)
|
||||
recent_article_field = author_field_type._meta.fields['recent_article']
|
||||
|
||||
assert recent_article_field.type() == Article
|
||||
assert recent_article_field.type == Article
|
||||
|
||||
feed_field = Query._meta.fields['feed']
|
||||
assert feed_field.type.of_type == Article
|
||||
|
|
Loading…
Reference in New Issue
Block a user