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

How can I build a custom node resolver? #690

Open
jerinpetergeorge opened this issue Jan 13, 2025 · 1 comment
Open

How can I build a custom node resolver? #690

jerinpetergeorge opened this issue Jan 13, 2025 · 1 comment

Comments

@jerinpetergeorge
Copy link

How can I build a custom node resolver for Global Object Identification with an XYZ unique identifier?

Consider that I defined the type(s) as below.

@strawberry_django.type(models.User)
class User(relay.Node):
    username: auto
    first_name: auto
    last_name: auto
    email: auto

@strawberry.type
class UserQuery:
    """
    User query class for GraphQL
    """

    users: strawberry.relay.ListConnection[User] = strawberry_django.connection(
        resolver=resolve_users
    )

By default, I can query a specific User node using its Global ID like this -

query{
  node(id:"VXNlcjox"){
    ... on User{
      username
      firstName
      email
    }
  }
}

But, how can I customize the behavior in a way that the node(...) should be able to accept other than the Global ID (like pk or any arbitrary field on all models in Django)

Example: (See customUniqueID parameter)

query{
  node(customUniqueID:"user_1234asdzxc"){
    ... on User{
      username
      firstName
      email
    }
  }
}

or (see anythingGoesHere(...) query along with customUniqueID)

query{
  anythingGoesHere(customUniqueID:"user_1234asdzxc"){
    ... on User{
      username
      firstName
      email
    }
  }
}

Is this doable?

@bellini666
Copy link
Member

Hrm, for now you would probably need to subclass the node implementation and override it.

TBF, the implementation we have for node is pretty straight forward: it accepts a GlobalID, parses it (which comes in the form of <TypeName>:<type-id> and, in case of strawberry-django, it uses the TypeName to find which model to query and the type-id for the object id itself.

So in theory you could define your own node resolver, which returns a Node, accepts a GlobalID, and parse it the way you prefer.

Having said that, what is your use case here? Interested to understand if maybe we should allow customization for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants