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.

Document resolvers

See original GitHub issue

I just learned, completely by accident, one of the most critically useful features of Graphene: resolvers can return anything, even for object resolvers! They don’t have to return instances of graphene.ObjectType. So, this works:

class TestInstance:
    x = 1
    y = 2

class Test(graphene.ObjectType):
    x = graphene.Int()
    y = graphene.Int()

class Query(graphene.ObjectType):
    test = Test()
    def resolve_test(self, info):
        return TestInstance()

While this is hinted at in the ObjectType reference docs, there are no code examples of the pattern, nor documentation of any kind about the behavior of resolver functions. Documentation about this feature, and guidance about nested Objects, should be added.

I’m trying to draft something but I don’t have a lot of time these days so I wanted to file an issue just in case

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jkimbocommented, May 3, 2018

@altaurog yep I’ve come across this issue before with the Node type. The way we worked around it was to create our own custom Relay.Node type and use that everywhere instead. That then gives you the opportunity to define a resolve_type method on the Node interface that can translate your underlying data structure into a Graphene type. E.g:

class RelayNode(relay.Node):
    class Meta:
        name = 'Node'
        description = 'An object with an ID'

    @classmethod
    def resolve_type(cls, instance, info):
        if isinstance(instance, TestInstance):
            return Test

        return cls.resolve_type(instance, info)
1reaction
jkimbocommented, May 7, 2021

Got it, but what is the best practice? Is there a rule of thumb which one should I use?

Depends on how your data is modelled really. Using the ObjectType as a container for your data doesn’t provide any benefits really.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolvers - Apollo GraphQL Docs
To accomplish this, it uses resolvers. A resolver is a function that's responsible for populating the data for a single field in your...
Read more >
About a Document Resolver Service
A document resolver service is a service that you create to perform duplicate detection for messages received by a JMS trigger or documents...
Read more >
Resolvers – GraphQL Tools
Resolvers are per field functions that are given a parent object, arguments, and the execution context, and are responsible for returning a ...
Read more >
Resolvers | Apple Developer Documentation
Resolvers let the system translate one type to another automatically. The system provides resolvers to convert between integer, floating-point, Boolean, string, ...
Read more >
JavaScript resolvers overview - AWS AppSync
JavaScript resolvers overview for AWS AppSync. ... aws appsync evaluate-code \ --code file://code.js \ --function request \ --context file://context.json ...
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