Distinguish between python and graphql generics#3202
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #3202 +/- ##
=======================================
Coverage 96.59% 96.60%
=======================================
Files 481 481
Lines 29806 29826 +20
Branches 3670 3673 +3
=======================================
+ Hits 28791 28813 +22
+ Misses 832 830 -2
Partials 183 183 |
|
|
||
| # If this is a generic field, try to resolve it using its origin's | ||
| # specialized type_var_map | ||
| # TODO: should we check arguments here too? |
There was a problem hiding this comment.
I'd leave this for the future 😊
|
Thanks for adding the Here's a preview of the changelog: This release changes how we check for generic types. Previously, any type that For example the following type: @strawberry.type
class Edge[T]:
cursor: strawberry.ID
some_interna_value: strawberry.Private[T]Will not generate a generic type in the schema, as the typevar Here's the tweet text: |
CodSpeed Performance ReportMerging #3202 will not alter performanceComparing Summary
|
|
/pre-release |
|
this broke lazy generics, will check in the next few days :) |
Pre-release👋 Pre-release 0.212.0.dev.1699291750 [c82108e] has been released on PyPi! 🚀 poetry add strawberry-graphql==0.212.0.dev.1699291750 |
|
Beautiful! I can confirm it works for my use case. Thank you! 🙏 As expected, I have to add Worth adding a test case like this, if you agree this is legit usage? @strawberry.interface
class GenericInterface[T]:
data: strawberry.Private[T]
@strawberry.field
def value(self) -> str:
raise NotImplementedError()
@strawberry.type
class ImplementationOne(GenericInterface[str]):
@strawberry.field
def value(self) -> str:
return self.data
@strawberry.type
class ImplementationTwo(GenericInterface[bool]):
@strawberry.field
def value(self) -> str:
if self.data is True:
return "true"
return "false"
@strawberry.type
class TestQuery:
@strawberry.field
@staticmethod
async def generic_field() -> GenericInterface: # pyright: ignore[reportMissingTypeArgument]
return ImplementationOne(data="foo")interface GenericInterface {
value: String!
}
type ImplementationOne implements GenericInterface {
value: String!
}
type ImplementationTwo implements GenericInterface {
value: String!
}
type TestQuery {
genericField: GenericInterface!
} |
Apollo Federation Subgraph Compatibility Results
Learn more: |
|
/pre-release |
|
@kkom can you test this latest pre-release (once it is out)? All tests are passing now, except for when I pass a type (like your example but with |
|
Awesome, will do later in the evening! But the test case looks exactly like what I'm after, so it should be all good :) Thanks! |
|
/pre-release |
|
Just tested it, it works exactly as I wanted it to – thanks! It's been a pretty simple use case so far, I'll let you know if anything more complex doesn't work – but I don't expect any issues really. |
|
I'll merge this later today! So I can get @bellini666 to do a release for Strawberry Django 😊 |
|
@patrick91 Obviously my fault but... what is the expected way to force strawberry to generate these? Previously I was getting: Generated when I had only: And now they aren't. My client ( |
|
@AntonOfTheWoods you'd need a field on the generic type to also use the type var Other than that I don't a good workaround idea 🤔 |
|
Thanks... so I guess I'll just pin to < 0.212.0 or recreate the 30 or so types that this got rid of... sniff... |

So at the moment we treat any generic as a GraphQL generic, this is usually fine,
but there are some cases where this is not ideal, for example in something like this:
where no GraphQL field is actually generic.
This PR changes how we check for generics by checking field types and argument types.
Not 100% sure yet if the implementation is fine, we might have some weird edge cases
This should be a better approach for fixing @kkom use case in #3160