Minimum requirements
Make sure you have correctly set up Authentication & Authorization.Definition
Declaration
The@authenticated directive can be declared on Enums, field definitions, Interfaces, Objects, and Scalars.
Declaration on field definitions (Interface and Object fields)
When@authenticated is declared on an Object field definition, that specific field will be protected (require
authentication).
For example, given the following federated schema:
Object.id would be protected in the following operation:
Interface.id would be protected in the following operation
(but note that @authenticated declared on an Interface field does not protect the fields of its implementations):
Declaration on the “type level” (Enums, Interfaces, Objects, and Scalars)
When@authenticated is declared on the “type level”, all field definitions with that named type (the innermost
response type name) will require authentication to access.
For example, consider the following federated schema:
@authenticated has been declared on:
- The Enum “Enum”
- The Interface “Interface”
- The Object “ObjectB”
- The Scalar “Scalar”
Query.enumsrequires authentication because it returns type “Enum”, which is declared@authenticated.Query.interfacesrequires authentication because it returns type “Interface”, which is declared@authenticated.Query.objectAs.enumrequires authentication because it returns type “Enum”, which is declared@authenticated.Query.objectAs.scalarrequires authentication because it returns type “Scalar”, which is declared@authenticated.Query.objectBsrequires authentication because it returns type “ObjectB”, which is declared@authenticated.Query.scalarsrequires authentication because it returns type “Scalar”, which is declared@authenticated.
Federation
The@authenticated directive will always persist in the federated schema.
Consequently, if @authenticated is declared on a field definition in one subgraph, and another instance of the same
field definition (a shared field) is defined in another subgraph without @authenticated, then @authenticated will
still be declared on the federated field.
This also means that selecting this field will always require authentication, regardless of whether it would be
resolved from a subgraph that did not declare @authenticated.
This is shown in the example below:
Errors
In the event that an unauthenticated agent selects a non-nullable field that is declared @authenticated, an authorization error will be returned, and the entire data will be null (see Non-nullable authenticated data requested among unauthenticated data).Partial data (nullable authenticated data)
Imagine an unauthenticated agent selects a field that is declared@authenticated and the response type of that field is
nullable.
However, the agent also queries a field that is not declared @authenticated (nor are any potential nested fields).
In this event, an authorization error will still be returned, but the specific data that requires authentication will
be null, while the data not requiring authentication will be returned.
Consider the following federated graph and corresponding query: