mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-29 04:53:55 +03:00
Allow adding GraphQL types to Graphene schema
This commit is contained in:
parent
64af43748c
commit
8f7f2dc559
|
@ -23,6 +23,7 @@ from graphql import (
|
||||||
GraphQLObjectType,
|
GraphQLObjectType,
|
||||||
GraphQLSchema,
|
GraphQLSchema,
|
||||||
GraphQLString,
|
GraphQLString,
|
||||||
|
GraphQLType,
|
||||||
Undefined,
|
Undefined,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -106,6 +107,11 @@ class TypeMap(dict):
|
||||||
def add_type(self, graphene_type):
|
def add_type(self, graphene_type):
|
||||||
if inspect.isfunction(graphene_type):
|
if inspect.isfunction(graphene_type):
|
||||||
graphene_type = graphene_type()
|
graphene_type = graphene_type()
|
||||||
|
|
||||||
|
# If type is a GraphQLType from graphql-core then return it immediately
|
||||||
|
if isinstance(graphene_type, GraphQLType):
|
||||||
|
return graphene_type
|
||||||
|
|
||||||
if isinstance(graphene_type, List):
|
if isinstance(graphene_type, List):
|
||||||
return GraphQLList(self.add_type(graphene_type.of_type))
|
return GraphQLList(self.add_type(graphene_type.of_type))
|
||||||
if isinstance(graphene_type, NonNull):
|
if isinstance(graphene_type, NonNull):
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from textwrap import dedent
|
||||||
|
|
||||||
from graphql.type import (
|
from graphql.type import (
|
||||||
GraphQLArgument,
|
GraphQLArgument,
|
||||||
GraphQLEnumType,
|
GraphQLEnumType,
|
||||||
|
@ -270,3 +272,44 @@ def test_objecttype_with_possible_types():
|
||||||
assert graphql_type.is_type_of
|
assert graphql_type.is_type_of
|
||||||
assert graphql_type.is_type_of({}, None) is True
|
assert graphql_type.is_type_of({}, None) is True
|
||||||
assert graphql_type.is_type_of(MyObjectType(), None) is False
|
assert graphql_type.is_type_of(MyObjectType(), None) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_graphql_type():
|
||||||
|
"""Type map should allow direct GraphQL types"""
|
||||||
|
MyGraphQLType = GraphQLObjectType(
|
||||||
|
name="MyGraphQLType",
|
||||||
|
fields={
|
||||||
|
"hello": GraphQLField(GraphQLString, resolve=lambda obj, info: "world")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
graphql_type = Field(MyGraphQLType)
|
||||||
|
|
||||||
|
def resolve_graphql_type(root, info):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
schema = Schema(query=Query)
|
||||||
|
assert str(schema) == dedent(
|
||||||
|
"""\
|
||||||
|
type Query {
|
||||||
|
graphqlType: MyGraphQLType
|
||||||
|
}
|
||||||
|
|
||||||
|
type MyGraphQLType {
|
||||||
|
hello: String
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
results = schema.execute(
|
||||||
|
"""
|
||||||
|
query {
|
||||||
|
graphqlType {
|
||||||
|
hello
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
assert not results.errors
|
||||||
|
assert results.data == {"graphqlType": {"hello": "world"}}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user