Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}
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(
name='Film',
parents=['FilmFields'],
fields=[
ParsedField(name='id', type='str', nullable=False),
]
)
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(
name='CreateHeroPayload',
fields=[
ParsedField(name='hero', type='Hero', nullable=True),
ParsedField(name='ok', type='bool', nullable=True)
],
children=[
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)
],
children=[
ParsedObject(
name='Film',
fields=[
ParsedField(name='title', type='str', nullable=False),
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(
name='Person',
parents=['CharacterFields'],
fields=[]
)
],
),
}
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=[
ParsedObject(
name='FilmConnection',
fields=[
ParsedField(name='count', type='int', nullable=True),
ParsedField(name='edges', type='List[FilmEdge]', nullable=True),
],
returnOfTheJedi: film(id: "1") {
... 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(
name='Film',
fields=[
ParsedField(name='title', type='str', nullable=False),
ParsedField(name='director', type='str', nullable=False),
]
)
authorAssociation
}
}
}
}
}
"""
parsed = github_parser.parse(query)
expected = asdict(ParsedQuery(
query=query,
enums=[
ParsedEnum(name='CommentAuthorAssociation', values={'MEMBER': 'MEMBER', 'OWNER': 'OWNER', 'COLLABORATOR': 'COLLABORATOR', 'CONTRIBUTOR': 'CONTRIBUTOR', 'FIRST_TIME_CONTRIBUTOR': 'FIRST_TIME_CONTRIBUTOR', 'FIRST_TIMER': 'FIRST_TIMER', 'NONE': 'NONE'})
],
objects=[
ParsedOperation(
name='MyIssues',
type='query',
children=[
ParsedObject(
name='MyIssuesData',
fields=[
ParsedField(name='viewer', type='User', nullable=False)
],
children=[
ParsedObject(
name='User',
fields=[
ParsedField(name='issues', type='IssueConnection', nullable=False)
],
children=[
ParsedObject(
def enter_operation_definition(self, node: OperationDefinitionNode, *_args):
name, operation = node.name, node.operation
variables = []
for var in node.variable_definitions:
ptype, nullable, _ = self.__variable_type_to_python(var.type)
variables.append(ParsedVariableDefinition(
name=var.variable.name.value,
type=ptype,
nullable=nullable,
default_value=var.default_value.value if var.default_value else None,
))
parsed_op = ParsedOperation(
name=name.value,
type=str(operation.value),
variables=variables,
children=[
ParsedObject(name=f'{name.value}Data')
]
)
self.parsed.objects.append(parsed_op) # pylint:disable=no-member
self.push(parsed_op)
self.push(parsed_op.children[0]) # pylint:disable=unsubscriptable-object
return node
buffer.write('')
self.__render_datetime_field(buffer)
# Enums
if parsed_query.enums:
self.__render_enum_field(buffer)
for enum in parsed_query.enums:
self.__render_enum(buffer, enum)
sorted_objects = sorted(parsed_query.objects, key=lambda obj: 1 if isinstance(obj, ParsedOperation) else 0)
for obj in sorted_objects:
if isinstance(obj, ParsedObject):
self.__render_object(parsed_query, buffer, obj)
elif isinstance(obj, ParsedOperation):
self.__render_operation(parsed_query, buffer, obj)
return str(buffer)
class ParsedVariableDefinition:
name: str
type: str
nullable: bool
default_value: Any = None
@dataclass
class ParsedOperation:
name: str
type: str
variables: List[ParsedVariableDefinition] = field(default_factory=list)
children: List[ParsedObject] = field(default_factory=list)
NodeT = Union[ParsedOperation, ParsedObject]
@dataclass
class ParsedQuery:
query: str
objects: List[NodeT] = field(default_factory=list)
enums: List[ParsedEnum] = field(default_factory=list)
class FieldToTypeMatcherVisitor(Visitor):
def __init__(self, schema: GraphQLSchema, type_info: TypeInfo, query: str):
self.schema = schema
self.type_info = type_info
self.query = query
self.parsed = ParsedQuery(query=self.query)