Python graphql.validate() Examples
The following are 12
code examples of graphql.validate().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
graphql
, or try the search function
.
Example #1
Source File: __init__.py From gql with MIT License | 5 votes |
def validation_errors(self, ast): return validate(self.get_schema(), ast)
Example #2
Source File: translator.py From edgedb with Apache License 2.0 | 5 votes |
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)
Example #3
Source File: test_validate_invalid_gql.py From graphql-core with MIT License | 5 votes |
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)]}, ]
Example #4
Source File: test_validate_gql.py From graphql-core with MIT License | 5 votes |
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 == []
Example #5
Source File: test_subscription_manager.py From graphql-python-subscriptions with MIT License | 5 votes |
def test_should_allow_a_valid_subscription(validation_schema): sub = 'subscription S1{ test1 }' errors = validate(validation_schema, parse(sub), [SubscriptionHasSingleRootField]) assert len(errors) == 0
Example #6
Source File: test_subscription_manager.py From graphql-python-subscriptions with MIT License | 5 votes |
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
Example #7
Source File: test_subscription_manager.py From graphql-python-subscriptions with MIT License | 5 votes |
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.'
Example #8
Source File: test_subscription_manager.py From graphql-python-subscriptions with MIT License | 5 votes |
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'
Example #9
Source File: test_subscription_manager.py From graphql-python-subscriptions with MIT License | 5 votes |
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'
Example #10
Source File: __init__.py From graphql-django-view with MIT License | 5 votes |
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)
Example #11
Source File: test_validation.py From graphql-core-legacy with MIT License | 5 votes |
def expect_valid(schema, query_string): errors = validate(schema, parse(query_string)) assert not errors
Example #12
Source File: translator.py From edgedb with Apache License 2.0 | 3 votes |
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, )