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.

Node fields and ids with clean URLs

See original GitHub issue

I’m using Graphene with Django and Relay (and react-router-relay), but struggling to figure out a good way of:

  • Having an entry point of say /thread/:id/ where id is the plain ID of a Django model instance (ie. a simple number like 7, so /thread/7/)
  • Using this with GraphQL fragments to query a node where id needs to be the opaque global ID, eg:
  fragment on Viewer {
     thread(id: "VGhyZWFkVHlwZToxMjU2OQ==") {
        ...
     }
   }

With the way Graphene-Django works when you modify your schemas for Relay, there doesn’t seem to be any way to query a node without that global ID.

Is this intentional? If so, how are you supposed to get from one form of ID to the other in your logic? The project I’m working on currently works with fine with the global ID in the URL, but that’s a really ugly and long URL in comparison to just the simple ID.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
khankuancommented, Apr 13, 2017

@matthewjohnston4 so far here is my understanding of using Node and filters w/o relay:

from graphene import Node
from graphene_django.types import DjangoObjectType

# Custom node to get rid of global id
class CustomNode(Node):
    class Meta:
        name = 'Node'

    @staticmethod
    def to_global_id(type, id):
        return id

    @staticmethod
    def get_node_from_global_id(id, context, info, only_type=None):
        return info.return_type.graphene_type._meta.model.objects.get(id=id)

# My example model
class MyModelType(DjangoObjectType):
    class Meta:
        model = MyModel
        interfaces = (CustomNode, )
        filter_fields = {
            'title': ['exact', 'icontains', 'istartswith'],
        }

# Query Schema
class Query(ObjectType):
    my_model_by_id = CustomNode.Field(MyModelType)
    all_my_model = DjangoFilterConnectionField(MyModelType)

@syrusakbary any thoughts of how to decouple filtering/node from relay?

0reactions
stale[bot]commented, Jun 11, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are IDs showing in URLs when clean URLs are enabled?
The Clean URLs setting turns your URLs from index.php?q=node/123 to /node/123 . The path module that comes with core Drupal enables the URL ......
Read more >
Clean URLs (possibly with slug) - node.js - Stack Overflow
Works fine. But is there any way to auto increment Numbers and save a custom ID (with a field name like, serial_number)?. I...
Read more >
How to see the Node ID (nid) of a given Node in Drupal 7 if ...
It is quite simple really, but not the most obvious. When you first install Drupal and create nodes (with Clean URLs turned on),...
Read more >
Replace serial entity IDs with UUIDs in URLs, or even globally?
Also this system would be half-baked without auto-increment field (serial field) because a lot of systems already use serial node IDs for ...
Read more >
When to use Drupal paths instead of aliases - InterWorks
Putting in node IDs ensures that the menu will always link to the correct path alias. Path redirects would keep users from encountering...
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