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.

Allow elements in edges to be marked as NonNull

See original GitHub issue

It seems that an edge, within the connection edges, cannot be marked as NonNull. I believe this is the result of https://github.com/graphql-python/graphene/blob/master/graphene/relay/connection.py#L103 where the edges field is hard coded to NonNull(List(edge)).

...
"edges",
Field(
    NonNull(List(edge)),
    description="Contains the nodes in this connection.",
),
...

This request is motivated by the following use case where we have a collection of objects, which we can guarantee are never null.

See the following for a contrived example:

type PostConnection {
  pageInfo: PageInfo!
  edges: [PostEdge]!
}

Since we know that a PostEdge, if found, will never be null we would like to be able to specify the following:

type PostConnection {
  pageInfo: PageInfo!
  edges: [PostEdge!]!
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:20
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
p7gcommented, Oct 2, 2019

This can be nicely wrapped up in a subclass of Connection:

class NonNullConnection(graphene.relay.Connection, abstract=True):
    @classmethod
    def __init_subclass_with_meta__(cls, node, **kwargs):
        if not hasattr(cls, 'Edge'):
            _node = node
            class EdgeBase(graphene.ObjectType, name=f'{node._meta.name}Edge'):
                cursor = graphene.String(required=True)
                node = graphene.Field(_node, required=True)

            setattr(cls, 'Edge', EdgeBase)

        if not hasattr(cls, 'edges'):
            setattr(cls, 'edges',
                    graphene.List(graphene.NonNull(cls.Edge), required=True))

        super(NonNullConnection, cls).__init_subclass_with_meta__(
            node=_node,
            **kwargs
        )

Which can be used like this (assuming a type called User):

class UserConnection(NonNullConnection):
    class Meta:
        node = User

# or

UserConnection = type('UserConnection', (NonNullConnection,), {}, node=User)

And results in a schema like this:

type UserConnection {
  pageInfo: PageInfo!
  edges: [UserEdge!]!
}

type UserEdge {
  cursor: String!
  node: User!
}
3reactions
ChazZeromuscommented, Sep 9, 2019

Agreed, the way graphene handles automatic connection schema creation makes it difficult to specify non-null edges in the list.

At least in graphene’s Connection class, it’s nice that it considers if there’s an Edge class already defined so I’m able to slightly do a bit of overriding to get the element non null. This will work:

class PostConnection(graphene.Connection):
    class Meta:
        node = Post

    class PostEdge(graphene.ObjectType):
        node = graphene.Field(Post, required=True)
        cursor = graphene.String(required=True)

    edges = graphene.List(graphene.NonNull(PostEdge), required=True)

Produces:

type PostConnection {
  """Pagination data for this connection."""
  pageInfo: PageInfo!
  edges: [PostEdge!]!
}

type PostEdge {
  node: Post!
  cursor: String!
}

You just gotta add the descriptions back in 😃

Not sure what would be the nicest way to make the edge element non-null if doing such a thing was supported, maybe a meta field on the connection class?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[RFC] Client controlled nullability operator · Issue #867 - GitHub
A "list operator" represented by [] which allows the above two operators to be applied to the elements of a list, or the...
Read more >
Resolve nullable warnings | Microsoft Learn
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >
java - How to use @Nullable and @Nonnull annotations more ...
The code below causes a parameter marked with @Nonnull to be null without raising any complaints. It throws a NullPointerException when it is...
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The value of obj.first is confirmed to be non- null (and non- undefined ) ... which allows passing an expression as the property...
Read more >
Modifier - Android Developers
Creates a LayoutModifier that allows changing how the wrapped element is ... Apply horizontal dp space along the left and right edges 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