question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validate resolver types against field types

See original GitHub issue

Current we don’t throw errors in cases like this:

def example() -> str:
    ...

@strawberry.type
class Q:
    x: int = strawberry.field(resolver=example)

We should raise an error when this happens. We should also maybe allow to change the error to a warning, to make it easier to upgrade.

We could also prevent this (maybe as opt-in/out behaviour):

def example():  # missing type
    ...

@strawberry.type
class Q:
    x: int = strawberry.field(resolver=example)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bikesheddercommented, Nov 25, 2021

I’m generating some of my resolvers dynamically. I don’t define the return type or use Any in this case. If you implement this feature please don’t forget this case as a resolver might return the right type at runtime but is unknown at the time it’s created.

0reactions
bikesheddercommented, Nov 25, 2021

In my code it looks somewhat like that:

def create_resolver(**filter):
    def resolver():
        # XXX In the actual code some DB query or similar is issued.
        return []
    return resolver

@strawberry.type
class Foo:
    bars: List['Bar'] = strawberry.field(resolver=create_resolver(type='bar'))

@strawberry.type
class Bar:
   name: str

In #1449 I added a unit test for that. The PR is about resolvers in different modules, but the test also includes the part of a generic resolver (no type annotation).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolvers - Apollo GraphQL Docs
Each resolver function belongs to whichever type its corresponding field belongs to. ... Here's a valid query against that schema: GraphQL.
Read more >
Resolvers - TypeGraphQL
Besides declaring GraphQL's object types, TypeGraphQL allows us to easily create queries, mutations and field resolvers - like normal class methods, ...
Read more >
Resolvers – GraphQL Tools
To respond to queries, a schema needs to have resolvers for all fields. Resolvers are per field functions that are given a parent...
Read more >
The Spec, Simplified: Validation & Execution
Introspection, which defines the query fields and types that GraphQL ... against fields in the schema and calls resolvers to generate the ...
Read more >
Custom Function Resolver examples for complex types
This is a one-time check used when compiling the application. Since data-types are invariant during execution, the one-time check is all that is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found