Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose fragments as a property on Info #3282

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: minor

Exposes fragments as a property on Info, which lets you access queried fragments without having to go through _raw_info.
23 changes: 12 additions & 11 deletions docs/types/resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,18 @@ Info objects contain information for the current execution context:

`class Info(Generic[ContextType, RootValueType])`

| Parameter name | Type | Description |
| --------------- | ------------------------- | --------------------------------------------------------------------- |
| field_name | `str` | The name of the current field (generally camel-cased) |
| python_name | `str` | The 'Python name' of the field (generally snake-cased) |
| context | `ContextType` | The value of the context |
| root_value | `RootValueType` | The value for the root type |
| variable_values | `Dict[str, Any]` | The variables for this operation |
| operation | `OperationDefinitionNode` | The ast for the current operation (public API might change in future) |
| path | `Path` | The path for the current field |
| selected_fields | `List[SelectedField]` | Additional information related to the current field |
| schema | `Schema` | The Strawberry schema instance |
| Parameter name | Type | Description |
| --------------- | ----------------------------------- | --------------------------------------------------------------------- |
| field_name | `str` | The name of the current field (generally camel-cased) |
| python_name | `str` | The 'Python name' of the field (generally snake-cased) |
| context | `ContextType` | The value of the context |
| root_value | `RootValueType` | The value for the root type |
| variable_values | `Dict[str, Any]` | The variables for this operation |
| operation | `OperationDefinitionNode` | The ast for the current operation (public API might change in future) |
| path | `Path` | The path for the current field |
| selected_fields | `List[SelectedField]` | Additional information related to the current field |
| schema | `Schema` | The Strawberry schema instance |
| fragments | `Dict[str, FragmentDefinitionNode]` | Fragments queried for during the current operation |

[^1]:
see
Expand Down
6 changes: 5 additions & 1 deletion strawberry/types/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

if TYPE_CHECKING:
from graphql import GraphQLResolveInfo, OperationDefinitionNode
from graphql.language import FieldNode
from graphql.language import FieldNode, FragmentDefinitionNode
from graphql.pyutils.path import Path

from strawberry.arguments import StrawberryArgument
Expand Down Expand Up @@ -73,6 +73,10 @@ def root_value(self) -> RootValueType:
def variable_values(self) -> Dict[str, Any]:
return self._raw_info.variable_values

@property
def fragments(self) -> Dict[str, FragmentDefinitionNode]:
return self._raw_info.fragments

@property
def return_type(
self,
Expand Down
5 changes: 5 additions & 0 deletions tests/schema/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Result:
operation: str
path: str
variable_values: str
fragments: str
context_equal: bool
root_equal: bool
return_type: str
Expand All @@ -40,6 +41,7 @@ def hello_world(self, info: Info[str, str]) -> Result:
python_name=info.python_name,
selected_field=json.dumps(dataclasses.asdict(*info.selected_fields)),
variable_values=str(info.variable_values),
fragments=str(info.fragments),
context_equal=info.context == my_context,
root_equal=info.root_value == root_value,
return_type=str(info.return_type),
Expand All @@ -53,6 +55,7 @@ def hello_world(self, info: Info[str, str]) -> Result:
fieldName
pythonName
selectedField
fragments
contextEqual
operation
path
Expand All @@ -77,6 +80,7 @@ def hello_world(self, info: Info[str, str]) -> Result:
"operation",
"contextEqual",
"variableValues",
"fragments",
"returnType",
"fieldName",
"pythonName",
Expand All @@ -95,6 +99,7 @@ def hello_world(self, info: Info[str, str]) -> Result:
"contextEqual": True,
"rootEqual": True,
"variableValues": "{}",
"fragments": "{}",
"returnType": "<class 'tests.schema.test_info.test_info_has_the_correct_shape.<locals>.Result'>",
"schemaPrint": schema.as_str(),
}
Expand Down