本文整理汇总了Python中graphene.ObjectType方法的典型用法代码示例。如果您正苦于以下问题:Python graphene.ObjectType方法的具体用法?Python graphene.ObjectType怎么用?Python graphene.ObjectType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphene
的用法示例。
在下文中一共展示了graphene.ObjectType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testQuery_excludedField
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def testQuery_excludedField(self):
Article(headline="h1", summary="s1").put()
class ArticleType(NdbObjectType):
class Meta:
model = Article
exclude_fields = ['summary']
class QueryType(graphene.ObjectType):
articles = graphene.List(ArticleType)
def resolve_articles(self, info):
return Article.query()
schema = graphene.Schema(query=QueryType)
query = '''
query ArticlesQuery {
articles { headline, summary }
}
'''
result = schema.execute(query)
self.assertIsNotNone(result.errors)
self.assertTrue('Cannot query field "summary"' in result.errors[0].message)
示例2: get_service_query
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def get_service_query(schema):
sdl_str = get_sdl(schema, custom_entities)
class _Service(ObjectType):
sdl = String()
def resolve_sdl(parent, _):
return sdl_str
class ServiceQuery(ObjectType):
_service = Field(_Service, name="_service")
def resolve__service(parent, info):
return _Service()
return ServiceQuery
示例3: test_get_session
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_get_session():
session = "My SQLAlchemy session"
class Query(ObjectType):
x = String()
def resolve_x(self, info):
return get_session(info.context)
query = """
query ReporterQuery {
x
}
"""
schema = Schema(query=Query)
result = schema.execute(query, context_value={"session": session})
assert not result.errors
assert result.data["x"] == session
示例4: test_objecttype_registered
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_objecttype_registered():
assert issubclass(Character, ObjectType)
assert Character._meta.model == Reporter
assert set(Character._meta.fields.keys()) == set(
[
"id",
"first_name",
"last_name",
"email",
"embedded_articles",
"embedded_list_articles",
"articles",
"awards",
"generic_reference",
"generic_embedded_document",
"generic_references",
]
)
示例5: test_should_filter_by_id
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_should_filter_by_id(fixtures):
# Notes: https://goo.gl/hMNRgs
class Query(graphene.ObjectType):
reporter = Node.Field(nodes.ReporterNode)
query = """
query ReporterQuery {
reporter (id: "UmVwb3J0ZXJOb2RlOjE=") {
id,
firstName,
awards
}
}
"""
expected = {
"reporter": {
"id": "UmVwb3J0ZXJOb2RlOjE=",
"firstName": "Allen",
"awards": ["2010-mvp"],
}
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)
assert not result.errors
assert result.data == expected
示例6: test_does_not_auto_camel_case
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_does_not_auto_camel_case(self):
# a query to test with a snake case field
class TestQuery(ObjectType):
test_field = String()
def resolve_test_field(self, args, info):
return 'hello'
# assign the query to the schema
self.schema.query = TestQuery
# the query to test
test_query = "query {test_field}"
# execute the query
resolved_query = self.schema.execute(test_query)
assert 'test_field' in resolved_query.data, (
"Schema did not have snake_case field."
)
assert resolved_query.data['test_field'] == 'hello', (
"Snake_case field did not have the right value"
)
示例7: testQuery_onlyFields
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def testQuery_onlyFields(self):
Article(headline="h1", summary="s1").put()
class ArticleType(NdbObjectType):
class Meta:
model = Article
only_fields = ['headline']
class QueryType(graphene.ObjectType):
articles = graphene.List(ArticleType)
def resolve_articles(self, info):
return Article.query()
schema = graphene.Schema(query=QueryType)
query = '''
query ArticlesQuery {
articles { headline }
}
'''
result = schema.execute(query)
self.assertIsNotNone(result.data)
self.assertEqual(result.data['articles'][0]['headline'], 'h1')
query = '''
query ArticlesQuery {
articles { headline, summary }
}
'''
result = schema.execute(query)
self.assertIsNotNone(result.errors)
self.assertTrue('Cannot query field "summary"' in result.errors[0].message)
示例8: test_context_lifetime
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_context_lifetime(gql):
"""Check that `info.context` holds data during the connection."""
# Store ids of `info.context.scope` objects to check them later.
run_log: List[bool] = []
print("Setup GraphQL backend and initialize GraphQL client.")
# pylint: disable=no-self-use
class Query(graphene.ObjectType):
"""Root GraphQL query."""
ok = graphene.Boolean()
def resolve_ok(self, info):
"""Store `info.context.scope` id."""
run_log.append("fortytwo" in info.context)
if "fortytwo" in info.context:
assert info.context.fortytwo == 42, "Context has delivered wrong data!"
info.context.fortytwo = 42
return True
for _ in range(2):
print("Make connection and perform query and close connection.")
client = gql(query=Query, consumer_attrs={"strict_ordering": True})
await client.connect_and_init()
for _ in range(2):
await client.send(msg_type="start", payload={"query": "{ ok }"})
await client.receive(assert_type="data")
await client.receive(assert_type="complete")
await client.finalize()
# Expected run log: [False, True, False, True].
assert run_log[2] is False, "Context preserved between connections!"
assert run_log[0:2] == [False, True], "Context is not preserved in a connection!"
assert run_log[2:4] == [False, True], "Context is not preserved in a connection!"
示例9: resolve_type
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def resolve_type(cls, instance, info):
from .objecttype import ObjectType # NOQA
if isinstance(instance, ObjectType):
return type(instance)
示例10: __init_subclass_with_meta__
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def __init_subclass_with_meta__(
cls,
interfaces=(),
possible_types=(),
default_resolver=None,
_meta=None,
**options,
):
if not _meta:
_meta = ObjectTypeOptions(cls)
fields = {}
for interface in interfaces:
assert issubclass(
interface, Interface
), f'All interfaces of {cls.__name__} must be a subclass of Interface. Received "{interface}".'
fields.update(interface._meta.fields)
for base in reversed(cls.__mro__):
fields.update(yank_fields_from_attrs(base.__dict__, _as=Field))
assert not (possible_types and cls.is_type_of), (
f"{cls.__name__}.Meta.possible_types will cause type collision with {cls.__name__}.is_type_of. "
"Please use one or other."
)
if _meta.fields:
_meta.fields.update(fields)
else:
_meta.fields = fields
if not _meta.interfaces:
_meta.interfaces = interfaces
_meta.possible_types = possible_types
_meta.default_resolver = default_resolver
super(ObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
示例11: test_issue
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_issue():
class Query(graphene.ObjectType):
things = relay.ConnectionField(MyUnion)
with raises(Exception) as exc_info:
graphene.Schema(query=Query)
assert str(exc_info.value) == (
"Query fields cannot be resolved."
" IterableConnectionField type has to be a subclass of Connection."
' Received "MyUnion".'
)
示例12: _get_class
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def _get_class(obj: 'GRAPHENE_OBJECT_OR_CLASS') -> 'Type[graphene.ObjectType]':
if inspect.isclass(obj):
return obj
return obj.__class__ # only graphene-sqlalchemy<=2.2.0; pragma: no cover
示例13: setUp
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def setUp(self):
self.Mutation = type('jwt', (graphene.ObjectType,), {
name: mutation.Field() for name, mutation in
self.refresh_token_mutations.items()
})
super().setUp()
示例14: test_objecttype_registered
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_objecttype_registered():
assert issubclass(Character, ObjectType)
assert Character._meta.model == Reporter
assert list(Character._meta.fields.keys()) == [
'articles',
'awards',
'custom_map',
'email',
'favorite_article',
'first_name',
'id',
'last_name',
'pets']
示例15: test_object_type
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import ObjectType [as 别名]
def test_object_type():
assert issubclass(Human, ObjectType)
assert sorted(list(Human._meta.fields.keys())) == ['headline', 'id', 'pub_date', 'reporter']
assert is_node(Human)