strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4.01k stars 531 forks source link

type PageInfo should have a way to be marked as @shareable when using relay #3196

Open jasonx1218 opened 1 year ago

jasonx1218 commented 1 year ago

Feature Request Type

Errors being thrown

Non-shareable field "PageInfo.endCursor" is resolved from multiple subgraphs
Non-shareable field "PageInfo.hasNextPage" is resolved from multiple subgraphs
Non-shareable field "PageInfo.hasPreviousPage" is resolved from multiple subgraphs
Non-shareable field "PageInfo.startCursor" is resolved from multiple subgraphs

Description / Request

When using relay.connection, type PageInfo is automatically generated and part of the schema. There should be an easier way to mark these automated/built-in types as @shareable or possibly with other federated schema directives.

"""Information to aid in pagination."""
type PageInfo {
  """When paginating forwards, are there more items?"""
  hasNextPage: Boolean!

  """When paginating backwards, are there more items?"""
  hasPreviousPage: Boolean!

  """When paginating backwards, the cursor to continue."""
  startCursor: String

  """When paginating forwards, the cursor to continue."""
  endCursor: String
}

Currently using a workaround before writing the schema to file:

generated_schema = print_schema(schema).strip()
generated_schema = generated_schema.replace(
    "type PageInfo ", "type PageInfo @shareable "
)
generated_schema = generated_schema.replace(
    '["@key"]', '["@key", "@shareable"]'
)

with open("schema.graphql", "w+") as file:
    file.write(generated_schema)

which writes out the federated directive

schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@shareable"]) {

and the type

type PageInfo @shareable { ... }

Upvote & Fund

Fund with Polar

RonquilloAeon commented 8 months ago

I ran into this issue as well. I think something changed in the newer versions of strawberry. I didn't have this problem last year.

I created a new PageInfo type and explicitly added the Shareable directive to get around the issue.

@type(description="Information to aid in pagination.")
class PageInfo:
    has_next_page: bool = field(
        description="When paginating forwards, are there more items?",
        directives=[Shareable()],
    )
    has_previous_page: bool = field(
        description="When paginating backwards, are there more items?",
        directives=[Shareable()],
    )
    start_cursor: Optional[str] = field(
        description="When paginating backwards, the cursor to continue.",
        directives=[Shareable()],
    )
    end_cursor: Optional[str] = field(
        description="When paginating forwards, the cursor to continue.",
        directives=[Shareable()],
    )

@strawberry.type
class MyConnection(relay.ListConnection[MyNode]):
    page_info: PageInfo = field(
        description="Pagination data for this connection",
    )

    ...
patrick91 commented 8 months ago

@RonquilloAeon would have some time to make a tiny reproduction? that will help fixing this issue :D