Company
Date Published
Author
Jens Neuse
Word count
1615
Language
English
Hacker News points
None

Summary

The Viewer Pattern is a design approach used in GraphQL APIs where the API is modeled around a "viewer" - the currently authenticated user making the request. This pattern simplifies access control and authentication by adding a `viewer` field to the root of the schema, allowing for unified authorization rules across child fields. The pattern also enables multi-tenancy support by introducing a `tenant` field, which can be used to fetch data specific to the user and shop they are currently logged into. In a federated GraphQL API, the Viewer Pattern is implemented by using the `@key` directive to mark fields as "Entity Keys" that require joining with other Subgraphs. This enables teams to build Subgraphs that work well together without requiring custom authentication middleware or spreading access control rules across all fields in their schema. The pattern simplifies the implementation of Subgraphs and makes it easier for teams to define an "entry point" to their Subgraph, allowing for standardized access control and authentication.