Skip to content

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.

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 code
  • forceResolver: Forces generation of a resolver method

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")

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 subgraph
type InventoryItem @key(fields: "sku") {
id: ID!
sku: String!
}
# file subgraph
type InventoryItem @key(fields: "id tenantID") {
id: ID!
tenantID: UUID!
file: [File]
}

Specifies external fields required to resolve a field.

extend type Product @key(fields: "id") {
id: ID! @external
stock: Stock @requires(fields: "id")
}

Indicates fields that this service provides for an external type.

type PickingOrderItem {
inventoryItem: InventoryItem @provides(fields: "sku")
}

Marks fields that are defined in another service.

extend type User @key(fields: "id") {
id: ID! @external
email: String! @external
pickingOrders: [PickingOrder!]!
}

Extends a type that originates from another service.

extend type Product @key(fields: "id") {
# Fields added by this service
}

Indicates that a field can be resolved by multiple services.

type Product @key(fields: "id") {
id: ID!
name: String! @shareable
}

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"])

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

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
}
}
}
}
}

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.

// Using @include directive
const 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 information
const minimalResult = await client.query({
query,
variables: {
sku: "SKU-123",
includeStock: false
}
});
// Fetch with stock information
const detailedResult = await client.query({
query,
variables: {
sku: "SKU-123",
includeStock: true
}
});