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.

Does relay.NodeField support many args more than "id" ?

See original GitHub issue

Hi, guys

In graphene/relay/fields.py :

class NodeField(Field):
    # ...
    def id_fetcher(self, global_id, info):
        # ...
        return object_type.get_node(_id, info)

    def resolver(self, instance, args, info):
        global_id = args.get('id')
        return self.id_fetcher(global_id, info)

This define a get_node strictly, just global id could be used as arg.

There is an example about other scene:

http://graphql-swapi.parseapp.com

They define a more useful interface:

film(id: ID, filmID: ID): Film
person(id: ID, personID: ID): Person
planet(id: ID, planetID: ID): Planet
species(id: ID, speciesID: ID): Species
starship(id: ID, starshipID: ID): Starship
vehicle(id: ID, vehicleID: ID): Vehicle
node(id: ID!): Node

It has a ‘XXXID’ arg in every node field. This is just what I wanted. It would be useful where a NodeField can queried by other args not just id.

Any help would be appreciated. Thanks!

Refs:

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ulgenscommented, Jan 3, 2020

I made something for same need:

import graphene
from graphene.relay import Node
from graphene.types.field import Field


class SlugNodeField(Field):
    def __init__(self, node, type=False, deprecation_reason=None, name=None, **kwargs):
        assert issubclass(node, Node), "NodeField can only operate in Nodes"
        self.node_type = node
        self.field_type = type

        super(SlugNodeField, self).__init__(
            type or node,
            description="The ID of the object",
            id=graphene.ID(required=False),
            slug=graphene.String(required=False),
        )


class SlugNode(Node):
    @classmethod
    def Field(cls, *args, **kwargs):  # noqa: N802
        return SlugNodeField(cls, *args, **kwargs)

    @classmethod
    def node_resolver(cls, only_type, root, info, id=None, slug=None):
        if id and slug:
            raise ValueError("id and slug cannot be used together.")
        elif id:
            return cls.get_node_from_global_id(info, id, only_type=only_type)
        elif slug:
            model = only_type._meta.model
            return model.objects.get(slug=slug)
        else:
            raise ValueError("id or slug is required.")

In related query, it should be used as

class FilmQuery:
    film = SlugNode.Field(FilmNode)

Old interface: film(id: ID!): FilmNode New interface: film(id: ID, slug: String): FilmNode

0reactions
syrusakbarycommented, Jun 1, 2016

As @mjtamlyn commented, this will be probably not implemented in graphene.relay as is not in the relay spec.

Closing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL Global Object Identification Specification - Relay
The server must provide an interface called Node . That interface must include exactly one field, called id that returns a non-null ID...
Read more >
Extending query arguments in graphene/graphene_django
The answer turns out to be simple. To add arguments to the resolver, declare the arguments in the constructor of the field, like...
Read more >
How to implement a nodes type with IDs args in absinthe?
Yes, ben. Thanks for your work. Implementing a node interface when use Absinthe.Relay.Node is very easy, but I'm asking how to implement a...
Read more >
GraphQL: understanding node interface. - DEV Community ‍ ‍
NOTE: If two objects appear in the query, both implementing the Node with identical ids, then the two objects must be equal. Node...
Read more >
Key arguments in Apollo Client - Apollo GraphQL Docs
The Apollo Client cache can store multiple entries for a single schema field. By default, each entry corresponds to a different set of...
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