Replies: 5 comments
-
See this thread on Spectrum: https://spectrum.chat/ariadne/general/lazy-resolve-of-fields~ca75fa83-1fc3-4ecd-9c5b-36e96615b640 |
Beta Was this translation helpful? Give feedback.
-
Marked it for docs - we glance over the |
Beta Was this translation helpful? Give feedback.
-
Thanks for the info. I found that the
Let me know if I can help on the doc and if there's a general guidelines. |
Beta Was this translation helpful? Give feedback.
-
Building on @suriyanto's solution, here is what I've come up with so far (Python 3.10+, but easily adaptable to older versions): def selected_fields(info: GraphQLResolveInfo) -> list[str]:
names: list[str] = []
for node in info.field_nodes:
if node.selection_set is None:
continue
names.extend(
_fields_from_selections(info, node.selection_set.selections)
)
return names
def _fields_from_selections(
info: GraphQLResolveInfo, selections: Iterable[SelectionNode]
) -> list[str]:
names: list[str] = []
for selection in selections:
match selection:
case FieldNode():
names.append(selection.name.value)
case InlineFragmentNode():
names.extend(
_fields_from_selections(
info, selection.selection_set.selections
)
)
case FragmentSpreadNode():
fragment = info.fragments[selection.name.value]
names.extend(
_fields_from_selections(
info, fragment.selection_set.selections
)
)
case _:
raise NotImplementedError(
f"field type {type(selection)} not supported"
)
return names It would be great if we could get such a utility function/method into ariadne proper. |
Beta Was this translation helpful? Give feedback.
-
If you want go deeper: from collections.abc import Iterable
from graphql import GraphQLResolveInfo
from graphql.language import (
FieldNode,
SelectionNode,
InlineFragmentNode,
FragmentSpreadNode,
)
def selected_fields(info: GraphQLResolveInfo) -> list[str]:
names: list[str] = []
for node in info.field_nodes:
if node.selection_set is None:
continue
names.extend(_fields_from_selections(info, node.selection_set.selections))
return set(names)
def _fields_from_selections(
info: GraphQLResolveInfo, selections: Iterable[SelectionNode]
) -> set[str]:
names: list[str] = []
for selection in selections:
match selection:
case FieldNode():
names.append(selection.name.value)
# go deeper to the next level
if selection.selection_set is not None:
names.extend(
_fields_from_selections(
info, selection.selection_set.selections
)
)
# ---
case InlineFragmentNode():
names.extend(
_fields_from_selections(info, selection.selection_set.selections)
)
case FragmentSpreadNode():
fragment = info.fragments[selection.name.value]
names.extend(
_fields_from_selections(info, fragment.selection_set.selections)
)
case _:
raise NotImplementedError(f"field type {type(selection)} not supported")
return names |
Beta Was this translation helpful? Give feedback.
-
I'm using Ariadne to allow generic query on top of our flat database through SQL statements. As some of the tables could have up to 500 fields, for performance purposes, I would like to be able to know which output fields are requested so I won't be loading all the unnecessary bytes.
According to #218, I should be able to get the requested output fields through
info.field_nodes
, but I only get the top query field name as the only entry here. Below is some test code to reproduce.When I query using below:
I get both firstName and lastName in the response output, but the printout gives me just the following:
I'm using Ariadne 0.9.0.
Beta Was this translation helpful? Give feedback.
All reactions