Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
query ShouldFail {
allFilms {
totalCount
edges {
node {
title
nonExistingField
}
}
}
}
"""
parser = QueryParser(swapi_schema)
with pytest.raises(InvalidQueryError) as excinfo:
parser.parse(query)
print(str(excinfo))
def parse(self, query: str, should_validate: bool = True) -> ParsedQuery:
document_ast = parse(query)
operation = get_operation_ast(document_ast)
if not operation.name:
raise AnonymousQueryError()
if should_validate:
errors = validate(self.schema, document_ast)
if errors:
raise InvalidQueryError(errors)
type_info = TypeInfo(self.schema)
visitor = FieldToTypeMatcherVisitor(self.schema, type_info, query)
visit(document_ast, TypeInfoVisitor(type_info, visitor))
result = visitor.parsed
return result
def gql_decode(value, **_):
decoded, decoded_length = utf_8.decode(value)
decoded = decoded.replace('# coding: gql', '')
try:
bio = BytesIO(decoded.encode('utf-8'))
result = gql_transform(bio)
return result, decoded_length
except InvalidQueryError:
raise
except Exception as ex:
return decoded, decoded_length
click.echo(f'Parsing {filename} ... ', nl=False)
with open(filename, 'r') as fin:
query = fin.read()
try:
parsed = parser.parse(query)
rendered = renderer.render(parsed)
with open(target_filename, 'w') as outfile:
outfile.write(rendered)
click.secho('Success!', fg='bright_white')
except AnonymousQueryError:
click.secho('Failed!', fg='bright_red')
click.secho('\tQuery is missing a name', fg='bright_black')
safe_remove(target_filename)
except InvalidQueryError as invalid_err:
click.secho('Failed!', fg='bright_red')
click.secho(f'\t{invalid_err}', fg='bright_black')
safe_remove(target_filename)