# GraphQL directives

Audience: Programmer

GraphQL directives available in the pyck API

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

### `@goField`

Controls Go code generation for specific fields.

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

```graphql
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

### `@goModel`

Maps GraphQL types to specific Go models.

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

```graphql
enum FileReftype @goModel(model: "github.com/pyck-ai/pyck/backend/file/ent/gen/file.Reftype")
```

## 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:

### `@key`

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:

```graphql
# 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]
}
```

### `@requires`

Specifies external fields required to resolve a field.

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

### `@provides`

Indicates fields that this service provides for an external type.

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

### `@external`

Marks fields that are defined in another service.

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

### `@extends`

Extends a type that originates from another service.

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

### `@shareable`

Indicates that a field can be resolved by multiple services.

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

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

### `@link`

Imports federation specifications and features.

```graphql
extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.3",
    import: ["@key", "@shareable", "@provides", "@external", "@requires", "@extends"])
```

## Directive usage in queries

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

### `@include`

Conditionally includes fields based on a variable.

```graphql
query GetItem($showDetails: Boolean!) {
  inventoryItems(first: 1, where: { sku: "SKU-123" }) {
    edges {
      node {
        id
        sku
        # Only include details if requested
        data @include(if: $showDetails)
      }
    }
  }
}
```

### `@skip`

Conditionally skips fields based on a variable.

```graphql
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

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

```javascript
// 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
  }
});
```

## See also

- [Schema Overview](https://docs.pyck.cloud/latest/api/services.md) for federation architecture
- [Authentication](https://docs.pyck.cloud/latest/developers/authentication.md#sending-authenticated-requests) for how requests are authenticated
- [Apollo Federation Spec](https://www.apollographql.com/docs/federation/federated-types/federated-directives) for detailed federation directive documentation
