本文整理汇总了Python中graphene.Schema方法的典型用法代码示例。如果您正苦于以下问题:Python graphene.Schema方法的具体用法?Python graphene.Schema怎么用?Python graphene.Schema使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphene
的用法示例。
在下文中一共展示了graphene.Schema方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testQuery_excludedField
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [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: test_create_post
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def test_create_post():
query_string = """
mutation {
createPost(text: "Try this out") {
result {
__typename
}
}
}
"""
schema = graphene.Schema(query=Query, mutation=Mutations)
result = schema.execute(query_string)
assert not result.errors
assert result.data["createPost"]["result"]["__typename"] == "Success"
示例3: test_get_session
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [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: run_hook
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def run_hook(self,
app: FlaskUnchained,
bundles: List[Bundle],
unchained_config: Optional[Dict[str, Any]] = None,
) -> None:
"""
Create the root :class:`graphene.Schema` from queries, mutations, and types
discovered by the other hooks and register it with the Graphene Bundle.
"""
mutations = tuple(self.bundle.mutations.values())
queries = tuple(self.bundle.queries.values())
types = list(self.bundle.types.values())
self.bundle.root_schema = graphene.Schema(
query=queries and type('Queries', queries, {}) or None,
mutation=mutations and type('Mutations', mutations, {}) or None,
types=types or None)
示例5: test_should_filter_by_id
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [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_graphql_mutation_from_summary
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def test_graphql_mutation_from_summary(self):
# create a mock mutation summary
mock_summary = summarize_crud_mutation(model=MockModelService(), method="delete")
# create the mutation
mutation = graphql_mutation_from_summary(mock_summary)
# create a schema to test the mutation
mock_schema = graphene.Schema()
# get the corresponding object type
mutation_object = mock_schema.T(mutation)
mutation_fields = list(mutation_object.get_fields().keys())
# there should be one field named status
assert mutation_fields == ['status'], (
"Delete mutation did not have correct output"
)
示例7: test_graphql_mutation_with_object_io_from_summary
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def test_graphql_mutation_with_object_io_from_summary(self):
# create a mock mutation summary with a io that's an object
mock_summary = summarize_crud_mutation(model=MockModelService(), method="create")
# create the mutation
mutation = graphql_mutation_from_summary(mock_summary)
# create a schema to test the mutation
mock_schema = graphene.Schema()
# get the corresponding object type
mutation_object = mock_schema.T(mutation)
# make sure there is a resulting 'testModel' in the mutation
assert 'testModel' in mutation_object.get_fields(), (
"Generated create mutation from summary does not have a service record in its output."
)
# the fields of the mutation result
output_fields = set(mutation_object.get_fields()['testModel'].type.get_fields().keys())
# make sure the object has the right types
assert output_fields == {'date', 'name', 'id'}, (
"Mutation output did not have the correct fields."
)
示例8: test_does_not_auto_camel_case
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [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"
)
示例9: show
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def show(cli_ctx):
with cli_ctx.logger:
schema = graphene.Schema(
query=Queries,
mutation=Mutations,
auto_camelcase=False)
log.info('======== GraphQL API Schema ========')
print(str(schema))
示例10: init
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def init(app: web.Application) -> None:
app['admin.gql_executor'] = AsyncioExecutor()
app['admin.gql_schema'] = graphene.Schema(
query=Queries,
mutation=Mutations,
auto_camelcase=False)
示例11: test_noSchema_returns500
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def test_noSchema_returns500(self):
graphql_application = webapp2.WSGIApplication([
('/graphql', GraphQLHandler)
])
app = webtest.TestApp(graphql_application)
for method in (app.get, app.post):
response = method('/graphql', expect_errors=True)
self.assertEqual(response.status_int, 500)
self.assertEqual(response.json_body['errors'][0]['message'], 'GraphQL Schema is missing.')
示例12: testQuery_onlyFields
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [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)
示例13: schema
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def schema(self, **kwargs):
self._schema = graphene.Schema(**kwargs)
示例14: test_issue
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [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".'
)
示例15: test_issue
# 需要导入模块: import graphene [as 别名]
# 或者: from graphene import Schema [as 别名]
def test_issue():
query_string = """
query myQuery {
myField(input: {x: 1})
}
"""
schema = graphene.Schema(query=Query)
result = schema.execute(query_string)
assert not result.errors