-
-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extensions support to federation.field (#3239)
* Add extensions support to federation.field * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update tests/schema/extensions/test_input_mutation_federation.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Patrick Arminio <patrick.arminio@gmail.com>
- Loading branch information
1 parent
aac7717
commit e56ae59
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Release type: minor | ||
|
||
Adds an optional `extensions` parameter to `strawberry.federation.field`, with default value `None`. The key is passed through to `strawberry.field`, so the functionality is exactly as described [here](https://strawberry.rocks/docs/guides/field-extensions). | ||
|
||
Example: | ||
|
||
```python | ||
strawberry.federation.field(extensions=[InputMutationExtension()]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/schema/extensions/test_input_mutation_federation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import textwrap | ||
from typing_extensions import Annotated | ||
|
||
import strawberry | ||
from strawberry.field_extensions import InputMutationExtension | ||
|
||
|
||
@strawberry.federation.type | ||
class Fruit: | ||
name: str | ||
color: str | ||
|
||
|
||
@strawberry.federation.type | ||
class Query: | ||
@strawberry.field | ||
def hello(self) -> str: # pragma: no cover | ||
return "hi" | ||
|
||
|
||
@strawberry.federation.type | ||
class Mutation: | ||
@strawberry.federation.mutation(extensions=[InputMutationExtension()]) | ||
def create_fruit( | ||
self, | ||
name: str, | ||
color: Annotated[ | ||
str, | ||
strawberry.federation.argument( | ||
description="The color of the fruit", | ||
), | ||
], | ||
) -> Fruit: | ||
return Fruit( | ||
name=name, | ||
color=color, | ||
) | ||
|
||
|
||
schema = strawberry.federation.Schema(query=Query, mutation=Mutation) | ||
|
||
|
||
def test_schema(): | ||
expected = ''' | ||
input CreateFruitInput { | ||
name: String! | ||
"""The color of the fruit""" | ||
color: String! | ||
} | ||
type Fruit { | ||
name: String! | ||
color: String! | ||
} | ||
type Mutation { | ||
createFruit( | ||
"""Input data for `createFruit` mutation""" | ||
input: CreateFruitInput! | ||
): Fruit! | ||
} | ||
type Query { | ||
_service: _Service! | ||
hello: String! | ||
} | ||
scalar _Any | ||
type _Service { | ||
sdl: String! | ||
} | ||
''' | ||
assert str(schema).strip() == textwrap.dedent(expected).strip() | ||
|
||
|
||
def test_input_mutation(): | ||
result = schema.execute_sync( | ||
""" | ||
mutation TestQuery ($input: CreateFruitInput!) { | ||
createFruit (input: $input) { | ||
... on Fruit { | ||
name | ||
color | ||
} | ||
} | ||
} | ||
""", | ||
variable_values={ | ||
"input": { | ||
"name": "Dragonfruit", | ||
"color": "red", | ||
} | ||
}, | ||
) | ||
assert result.errors is None | ||
assert result.data == { | ||
"createFruit": { | ||
"name": "Dragonfruit", | ||
"color": "red", | ||
}, | ||
} |