Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
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(
name='Film',
fields=[
ParsedField(name='title', type='str', nullable=False),
ParsedField(name='director', type='str', nullable=False),
]
)
]
)
]
)
]
))
)
],
),
]
),
ParsedObject(
name='CharacterFields',
fields=[
ParsedField(name='name', type='str', nullable=False),
ParsedField(name='home', type='Planet', nullable=False),
],
children=[
ParsedObject(
name='Planet',
fields=[
ParsedField(name='name', type='str', nullable=False),
]
)
]
)
]
))
parsed_dict = asdict(parsed)
assert bool(parsed)
assert parsed_dict == expected, str(DeepDiff(parsed_dict, expected))
fields=[
ParsedField(name='allFilms', type='FilmConnection', nullable=True),
ParsedField(name='allHeroes', type='HeroConnection', nullable=True)
],
children=[
ParsedObject(
name='FilmConnection',
fields=[
ParsedField(name='count', type='int', nullable=True),
ParsedField(name='edges', type='List[FilmEdge]', nullable=True),
],
children=[
ParsedObject(
name='FilmEdge',
fields=[
ParsedField(name='node', type='Film', nullable=True)
],
children=[
ParsedObject(
name='Film',
fields=[
ParsedField(name='id', type='str', nullable=False),
ParsedField(name='title', type='str', nullable=False),
ParsedField(name='director', type='str', nullable=False)
]
)
]
)
]
),
ParsedObject(
objects=[
ParsedOperation(
name='GetFilm',
type='query',
children=[
ParsedObject(
name='GetFilmData',
fields=[
ParsedField(name='returnOfTheJedi', type='Film', nullable=True)
],
children=[
ParsedObject(
name='Film',
parents=['FilmFields'],
fields=[
ParsedField(name='id', type='str', nullable=False),
]
)
],
),
]
),
ParsedObject(
name='FilmFields',
fields=[
ParsedField(name='title', type='str', nullable=False),
ParsedField(name='director', type='str', nullable=False),
],
)
]
))
"""
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(
name='Film',
fields=[
ParsedField(name='title', type='str', nullable=False),
ParsedField(name='director', type='str', nullable=False),
]
)
],
)
]
)
]
))
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)
],
children=[
ParsedObject(
name='Film',
fields=[
ParsedField(name='title', type='str', nullable=False),
ParsedField(name='director', type='str', nullable=False),
]
)
]
)
]
)
]
))
objects=[
ParsedOperation(
name='CreateHero',
type='mutation',
children=[
ParsedObject(
name='CreateHeroData',
fields=[
ParsedField(name='createHero', type='CreateHeroPayload', nullable=True)
],
children=[
ParsedObject(
name='CreateHeroPayload',
fields=[
ParsedField(name='hero', type='Hero', nullable=True),
ParsedField(name='ok', type='bool', nullable=True)
],
children=[
ParsedObject(
name='Hero',
fields=[
ParsedField(name='name', type='str', nullable=False)
]
)
]
)
]
)
]
)
]
))
children=[
ParsedObject(
name='IssueEdge',
fields=[ParsedField(name='node', type='Issue', nullable=True)],
children=[
ParsedObject(
name='Issue',
fields=[
ParsedField(name='author', type='Actor', nullable=True),
ParsedField(name='authorAssociation', type='CommentAuthorAssociation', nullable=False)
],
children=[
ParsedObject(
name='Actor',
fields=[
ParsedField(name='login', type='str', nullable=False)
]
)
]
)
]
)
]
)
]
)
]
)
]
)
]
))
GraphQLNonNull, is_scalar_type, GraphQLList, OperationDefinitionNode, NonNullTypeNode, TypeNode, GraphQLEnumType, \
is_enum_type
@dataclass
class ParsedField:
name: str
type: str
nullable: bool
default_value: Any = None
@dataclass
class ParsedObject:
name: str
fields: List[ParsedField] = field(default_factory=list)
parents: List[str] = field(default_factory=list)
children: List['ParsedObject'] = field(default_factory=list)
@dataclass
class ParsedEnum:
name: str
values: Mapping[str, Any]
@dataclass
class ParsedVariableDefinition:
name: str
type: str
nullable: bool
default_value: Any = None
def enter_field(self, node, *_):
name = node.alias.value if node.alias else node.name.value
graphql_type = self.type_info.get_type()
python_type, nullable, underlying_graphql_type = self.__scalar_type_to_python(graphql_type)
parsed_field = ParsedField(
name=name,
type=python_type,
nullable=nullable,
)
self.current.fields.append(parsed_field) # TODO: nullables should go to the end
if not is_scalar_type(underlying_graphql_type):
if is_enum_type(underlying_graphql_type):
enum_type = cast(GraphQLEnumType, self.schema.type_map[underlying_graphql_type.name])
name = enum_type.name
if not any(e.name == name for e in self.parsed.enums): # pylint:disable=not-an-iterable
parsed_enum = ParsedEnum(
name=enum_type.name,
values={name: value.value or name for name, value in enum_type.values.items()}
)