本文整理汇总了Python中graphql.validate方法的典型用法代码示例。如果您正苦于以下问题:Python graphql.validate方法的具体用法?Python graphql.validate怎么用?Python graphql.validate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphql
的用法示例。
在下文中一共展示了graphql.validate方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validation_errors
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def validation_errors(self, ast):
return validate(self.get_schema(), ast)
示例2: _validate_fragment_type
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def _validate_fragment_type(self, frag, spread):
is_specialized = False
base_type = None
# validate the fragment type w.r.t. the base
if frag.type_condition is None:
return
# validate the base if it's nested
if len(self._context.path) > 0:
path = self._context.path[-1]
base_type = path[-1].type
frag_type = self.get_type(frag.type_condition.name.value)
if base_type.issubclass(frag_type):
# legal hierarchy, no change
pass
elif frag_type.issubclass(base_type):
# specialized link, but still legal
is_specialized = True
else:
raise g_errors.GraphQLValidationError(
f"{base_type.short_name} and {frag_type.short_name} " +
"are not related",
loc=self.get_loc(frag))
self._context.path.append([
Step(name=frag.type_condition, type=frag_type, eql_alias=None)])
self._context.include_base.append(is_specialized)
示例3: test_validate_invalid_query
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def test_validate_invalid_query(benchmark, big_schema_sdl): # noqa: F811
schema = build_schema(big_schema_sdl, assume_valid=True)
query_ast = parse(
"""
{
unknownField
... on unknownType {
anotherUnknownField
...unknownFragment
}
}
fragment TestFragment on anotherUnknownType {
yetAnotherUnknownField
}
"""
)
result = benchmark(lambda: validate(schema, query_ast))
assert result == [
{
"message": "Cannot query field 'unknownField' on type 'Query'.",
"locations": [(3, 11)],
},
{"message": "Unknown type 'unknownType'.", "locations": [(4, 18)]},
{"message": "Unknown fragment 'unknownFragment'.", "locations": [(6, 16)]},
{"message": "Unknown type 'anotherUnknownType'.", "locations": [(10, 34)]},
{"message": "Fragment 'TestFragment' is never used.", "locations": [(10, 9)]},
]
示例4: test_validate_introspection_query
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def test_validate_introspection_query(benchmark, big_schema_sdl): # noqa: F811
schema = build_schema(big_schema_sdl, assume_valid=True)
query = parse(get_introspection_query())
result = benchmark(lambda: validate(schema, query))
assert result == []
示例5: test_should_allow_a_valid_subscription
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def test_should_allow_a_valid_subscription(validation_schema):
sub = 'subscription S1{ test1 }'
errors = validate(validation_schema, parse(sub),
[SubscriptionHasSingleRootField])
assert len(errors) == 0
示例6: test_should_allow_another_valid_subscription
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def test_should_allow_another_valid_subscription(validation_schema):
sub = 'subscription S1{ test1 } subscription S2{ test2 }'
errors = validate(validation_schema, parse(sub),
[SubscriptionHasSingleRootField])
assert len(errors) == 0
示例7: test_should_not_allow_two_fields_in_the_subscription
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def test_should_not_allow_two_fields_in_the_subscription(validation_schema):
sub = 'subscription S3{ test1 test2 }'
errors = validate(validation_schema, parse(sub),
[SubscriptionHasSingleRootField])
assert len(errors) == 1
assert errors[0].message == 'Subscription "S3" must have only one field.'
示例8: test_should_not_allow_inline_fragments
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def test_should_not_allow_inline_fragments(validation_schema):
sub = 'subscription S4{ ...on Subscription { test1 } }'
errors = validate(validation_schema, parse(sub),
[SubscriptionHasSingleRootField])
assert len(errors) == 1
assert errors[0].message == 'Apollo subscriptions do not support\
fragments on the root field'
示例9: test_should_not_allow_fragments
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def test_should_not_allow_fragments(validation_schema):
sub = 'subscription S5{ ...testFragment }\
fragment testFragment on Subscription{ test2 }'
errors = validate(validation_schema, parse(sub),
[SubscriptionHasSingleRootField])
assert len(errors) == 1
assert errors[0].message == 'Apollo subscriptions do not support\
fragments on the root field'
示例10: execute_graphql_request
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def execute_graphql_request(self, request):
query, variables, operation_name = self.get_graphql_params(request, self.parse_body(request))
if not query:
raise HttpError(HttpResponseBadRequest('Must provide query string.'))
source = Source(query, name='GraphQL request')
try:
document_ast = parse(source)
validation_errors = validate(self.schema, document_ast)
if validation_errors:
return ExecutionResult(
errors=validation_errors,
invalid=True,
)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)
if request.method.lower() == 'get':
operation_ast = get_operation_ast(document_ast, operation_name)
if operation_ast and operation_ast.operation != 'query':
raise HttpError(HttpResponseNotAllowed(
['POST'], 'Can only perform a {} operation from a POST request.'.format(operation_ast.operation)
))
try:
return self.execute(
document_ast,
root_value=self.get_root_value(request),
variable_values=variables,
operation_name=operation_name,
context_value=self.get_context(request),
executor=self.executor,
)
except Exception as e:
return ExecutionResult(errors=[e], invalid=True)
示例11: expect_valid
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def expect_valid(schema, query_string):
errors = validate(schema, parse(query_string))
assert not errors
示例12: translate_ast
# 需要导入模块: import graphql [as 别名]
# 或者: from graphql import validate [as 别名]
def translate_ast(
gqlcore: gt.GQLCoreSchema,
document_ast: graphql.Document,
*,
operation_name: Optional[str]=None,
variables: Dict[str, Any]=None,
substitutions: Optional[Dict[str, Tuple[str, int, int]]],
) -> TranspiledOperation:
if variables is None:
variables = {}
validation_errors = convert_errors(
graphql.validate(gqlcore.graphql_schema, document_ast),
substitutions=substitutions)
if validation_errors:
err = validation_errors[0]
if isinstance(err, graphql.GraphQLError):
# possibly add additional information and/or hints to the
# error message
msg = augment_error_message(gqlcore, err.message)
err_loc = (err.locations[0].line, err.locations[0].column)
raise g_errors.GraphQLCoreError(msg, loc=err_loc)
else:
raise err
context = GraphQLTranslatorContext(
gqlcore=gqlcore, query=None,
variables=variables, document_ast=document_ast,
operation_name=operation_name)
edge_forest_map = GraphQLTranslator(context=context).visit(document_ast)
if debug.flags.graphql_compile:
for opname, op in sorted(edge_forest_map.items()):
print(f'== operationName: {opname!r} =============')
print(ql_codegen.generate_source(op.stmt))
op = next(iter(edge_forest_map.values()))
# generate the specific result
return TranspiledOperation(
edgeql_ast=op.stmt,
cacheable=True,
cache_deps_vars=frozenset(op.critvars) if op.critvars else None,
variables_desc=op.vars,
)