Directives

GraphQL directives available in the pyck API

Audience: Programmer

Directives provide a way to modify the execution of GraphQL queries and control schema behavior. pyck uses both standard federation directives and custom directives for various purposes.

Custom Directives

@public

Marks fields, queries, or mutations as publicly accessible without authentication.

Usage: Applied to schema elements that should be accessible without authentication.

type Query {
  # This query is accessible without authentication
  publicDataTypes: [DataType!]! @public
  
  # This query requires authentication (default)
  inventoryItems: InventoryItemConnection!
}

Note: Most of the pyck API requires authentication. Only specific endpoints marked with @public are accessible without credentials.

@goField

Controls Go code generation for specific fields.

Usage: Internal directive used by the code generator to customize field behavior in Go.

Parameters:

  • name: Custom field name in Go code

  • forceResolver: Forces generation of a resolver method

@goModel

Maps GraphQL types to specific Go models.

Usage: Internal directive for mapping GraphQL types to Go structs.

Federation Directives

pyck uses Apollo Federation v2.3 directives for distributed schema composition:

@key

Defines the primary key for an entity that can be resolved across services.

@requires

Specifies external fields required to resolve a field.

@provides

Indicates fields that this service provides for an external type.

@external

Marks fields that are defined in another service.

@extends

Extends a type that originates from another service.

@shareable

Indicates that a field can be resolved by multiple services.

Federation-Specific Join Directives

These directives are used internally by the federation runtime:

  • @join__enumValue: Maps enum values across services

  • @join__field: Configures field joining behavior

  • @join__graph: Identifies subgraph sources

  • @join__implements: Handles interface implementation across services

  • @join__type: Manages type definitions across services

  • @join__unionMember: Handles union members across services

Imports federation specifications and features.

Directive Usage in Queries

While most directives are schema-level, some can be used in queries:

@include

Conditionally includes fields based on a variable.

@skip

Conditionally skips fields based on a variable.

Best Practices

  1. Authentication: Assume all fields require authentication unless marked with @public

  2. Federation: Let the Gateway handle federation directives automatically

  3. Conditional Fields: Use @include/@skip for dynamic field selection

  4. Performance: Be mindful that conditional directives still affect query complexity

JavaScript Example

See Also

Last updated

Was this helpful?