Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_parser_query_with_variables(swapi_schema):
query = """
query GetFilm($theFilmID: ID!) {
returnOfTheJedi: film(id: $theFilmID) {
title
director
}
}
"""
parser = QueryParser(swapi_schema)
parsed = parser.parse(query)
expected = asdict(ParsedQuery(
query=query,
objects=[
ParsedOperation(
name='GetFilm',
type='query',
variables=[
ParsedVariableDefinition(name='theFilmID', type='str', nullable=False)
],
children=[
ParsedObject(
name='GetFilmData',
fields=[
ParsedField(name='returnOfTheJedi', type='Film', nullable=True)
edges {
node {
...HeroFields
}
}
}
}
fragment HeroFields on Hero {
id
name
}
"""
parser = QueryParser(swapi_schema)
parsed = parser.parse(query)
expected = asdict(ParsedQuery(
query=query,
objects=[
ParsedOperation(
name='GetAllFilms',
type='query',
children=[
ParsedObject(
name='GetAllFilmsData',
fields=[
ParsedField(name='allFilms', type='FilmConnection', nullable=True),
ParsedField(name='allHeroes', type='HeroConnection', nullable=True)
],
children=[
def test_parser_query_fragment(swapi_schema):
query = """
query GetFilm {
returnOfTheJedi: film(id: "1") {
id
...FilmFields
}
}
fragment FilmFields on Film {
title
director
}
"""
parser = QueryParser(swapi_schema)
parsed = parser.parse(query)
expected = asdict(ParsedQuery(
query=query,
objects=[
ParsedOperation(
name='GetFilm',
type='query',
children=[
ParsedObject(
name='GetFilmData',
fields=[
ParsedField(name='returnOfTheJedi', type='Film', nullable=True)
],
children=[
ParsedObject(
def test_parser_mutation(swapi_schema):
query = """
mutation CreateHero {
createHero {
hero {
name
}
ok
}
}
"""
parser = QueryParser(swapi_schema)
parsed = parser.parse(query)
expected = asdict(ParsedQuery(
query=query,
objects=[
ParsedOperation(
name='CreateHero',
type='mutation',
children=[
ParsedObject(
name='CreateHeroData',
fields=[
ParsedField(name='createHero', type='CreateHeroPayload', nullable=True)
],
children=[
ParsedObject(
def test_parser_fails_invalid_query(swapi_schema):
query = """
query ShouldFail {
allFilms {
totalCount
edges {
node {
title
nonExistingField
}
}
}
}
"""
parser = QueryParser(swapi_schema)
with pytest.raises(InvalidQueryError) as excinfo:
parser.parse(query)
print(str(excinfo))
query GetPerson {
luke: character(id: "luke") {
...CharacterFields
}
}
fragment CharacterFields on Person {
name
home: homeworld {
name
}
}
"""
parser = QueryParser(swapi_schema)
parsed = parser.parse(query)
expected = asdict(ParsedQuery(
query=query,
objects=[
ParsedOperation(
name='GetPerson',
type='query',
children=[
ParsedObject(
name='GetPersonData',
fields=[
ParsedField(name='luke', type='Person', nullable=True)
],
children=[
ParsedObject(
fields={
'people': GraphQLField(
GraphQLList(GraphQLNonNull(PersonType)),
resolve=lambda obj, info: {'name':'eran'}
)
}))
query = """
query GetPeople {
people {
name
}
}
"""
parser = QueryParser(schema)
dataclass_renderer = DataclassesRenderer(schema, Config(schema='schemaurl', endpoint='schemaurl', documents=''))
parsed = parser.parse(query)
rendered = dataclass_renderer.render(parsed)
m = module_compiler(rendered)
now = datetime.now()
call_mock = mocker.patch.object(m.Client, 'call')
call_mock.return_value = """
{
"data": {
"people": [
{
"name": "eran"
def get_parser() -> QueryParser:
if 'PARSER' not in globals():
schema = get_schema()
globals()['PARSER'] = QueryParser(schema)
return globals()['PARSER']
def __init__(self, config: Config, schema: GraphQLSchema):
self.parser = QueryParser(schema)
self.renderer = DataclassesRenderer(schema, config)