Skip to content

Extend print_definition to object, input, interface, and union types #4599

Description

@patrick91

After #4598, print_definition=False is supported on @strawberry.schema_directive (since #2047), strawberry.scalar, and strawberry.enum. Object types, input types, interfaces, and unions have no equivalent, so there is currently no way to keep those kinds of support types out of printed SDL while keeping them available to introspection and execution.

This issue tracks filling that gap so the visibility mechanism is consistent across every kind of definition.

Current behavior

A hidden directive whose argument is an input object still prints that input, because inputs cannot be hidden. The test test_print_definition_false_remains_available_to_introspection in tests/schema/test_schema_directives.py shows this:

@strawberry.input
class HiddenConfig:
    reason: str

@strawberry.schema_directive(
    locations=[Location.FIELD_DEFINITION], print_definition=False
)
class Hidden:
    config: HiddenConfig | None = strawberry.UNSET

directive @hidden is omitted as requested, but HiddenConfig — which exists only to serve that hidden directive — is force-printed. Today, this is the only option for input types, so the test asserts the current output as expected.

Proposed change (by claude)

Add print_definition: bool = True to @strawberry.type, @strawberry.input, @strawberry.interface, and strawberry.union, stored on StrawberryObjectDefinition / StrawberryUnion and consulted by _should_print_type in strawberry/printer/printer.py exactly as it already is for scalars and enums.

Expected details, matching the existing semantics:

  • Hidden types stay registered in the GraphQLSchema and remain visible to introspection; ition is omitted.
  • A hidden type that is referenced by a visible definition is still printed, so the emitted SDL stays valid (same reachability rule the printer applies to scalars and enums today).
  • The HiddenConfig case above should then be able to produce just type Query { name: Stri

Open questions

  • Interfaces: should hiding an interface also require hiding its implementations, or should reachability handle it? Reachability is probably enough, but it needs a test.
  • Unions: a hidden union referenced by a visible field will be printed by reachability; that's consistent, just worth documenting.
  • Whether a single generic, schema-level visibility hook would be preferable to per-decorator flags was discussed on Expose attached schema directives through introspection #4598. The conclusion was to stay consistent with the existing declaration-site print_definition convention rather than introduce a second mechanism.

I also wonder if we keep this API to change to to _print_definition since it's more about internal stuff.

Or we find a different way altogether to do this, which could also make it easier to implement schema visibility support 🤔

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions