GraphQL directives
Audience Developer
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
Section titled “Custom directives”@goField
Section titled “@goField”Controls Go code generation for specific fields.
Usage: Internal directive used by the code generator to customize field behavior in Go.
type InventoryItem { # Custom Go field configuration itemset: [InventoryItemSet!] @goField(name: "ItemSet", forceResolver: false)}Parameters:
name: Custom field name in Go codeforceResolver: Forces generation of a resolver method
@goModel
Section titled “@goModel”Maps GraphQL types to specific Go models.
Usage: Internal directive for mapping GraphQL types to Go structs.
enum FileReftype @goModel(model: "github.com/pyck-ai/pyck/backend/file/ent/gen/file.Reftype")Federation directives
Section titled “Federation directives”Pyck uses Apollo Federation directives for distributed schema composition; the gateway composes the supergraph with Federation version 2.15.2. of these directives, Pyck’s own subgraph schemas use only @key. The examples for the others, including @link, are generic illustrations of the syntax, not excerpts from Pyck’s schema:
Defines the primary key for an entity that can be resolved across services.
InventoryItem is an entity in two subgraphs, each with its own key:
# inventory subgraphtype InventoryItem @key(fields: "sku") { id: ID! sku: String!}
# file subgraphtype InventoryItem @key(fields: "id tenantID") { id: ID! tenantID: UUID! file: [File]}@requires
Section titled “@requires”Specifies external fields required to resolve a field.
extend type Product @key(fields: "id") { id: ID! @external stock: Stock @requires(fields: "id")}@provides
Section titled “@provides”Indicates fields that this service provides for an external type.
type PickingOrderItem { inventoryItem: InventoryItem @provides(fields: "sku")}@external
Section titled “@external”Marks fields that are defined in another service.
extend type User @key(fields: "id") { id: ID! @external email: String! @external pickingOrders: [PickingOrder!]!}@extends
Section titled “@extends”Extends a type that originates from another service.
extend type Product @key(fields: "id") { # Fields added by this service}@shareable
Section titled “@shareable”Indicates that a field can be resolved by multiple services.
type Product @key(fields: "id") { id: ID! name: String! @shareable}Federation-specific join directives
Section titled “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.
extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@shareable", "@provides", "@external", "@requires", "@extends"])Directive usage in queries
Section titled “Directive usage in queries”While most directives are schema-level, some can be used in queries:
@include
Section titled “@include”Conditionally includes fields based on a variable.
query GetItem($showDetails: Boolean!) { inventoryItems(first: 1, where: { sku: "SKU-123" }) { edges { node { id sku # Only include details if requested data @include(if: $showDetails) } } }}Conditionally skips fields based on a variable.
query GetItems($hideStock: Boolean!) { inventoryItems(first: 10) { edges { node { id sku # Skip stock info if not needed itemstocks @skip(if: $hideStock) { quantity incomingStock outgoingStock } } } }}Best practices
Section titled “Best practices”Assume all fields require authentication; Pyck has no directive that makes a field public. Let the Gateway handle federation directives automatically. Use @include/@skip for dynamic field selection, and be mindful that conditional directives still affect query complexity.
JavaScript example
Section titled “JavaScript example”// Using @include directiveconst query = gql` query GetInventoryItem($sku: String!, $includeStock: Boolean!) { inventoryItems(first: 1, where: { sku: $sku }) { edges { node { id sku itemstocks @include(if: $includeStock) { quantity incomingStock outgoingStock } } } } }`;
// Fetch without stock informationconst minimalResult = await client.query({ query, variables: { sku: "SKU-123", includeStock: false }});
// Fetch with stock informationconst detailedResult = await client.query({ query, variables: { sku: "SKU-123", includeStock: true }});See also
Section titled “See also”- Schema Overview for federation architecture
- Authentication for how requests are authenticated
- Apollo Federation Spec for detailed federation directive documentation