> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pylon.mortgage/llms.txt
> Use this file to discover all available pages before exploring further.

# Variables and fragments

> Learn how to use variables and fragments to make your GraphQL queries more reusable

## Variables

Variables allow you to pass dynamic values to your queries and mutations, making them reusable.

### Using variables

Define variables in your query and pass them separately:

```graphql theme={null}
query GetDeal($id: ID!) {
  deal(id: $id) {
    id
    friendlyId
  }
}
```

**Variables:**

```json theme={null}
{
  "id": "123"
}
```

### Variable types

GraphQL supports various variable types:

* `ID!` - Required ID
* `String` - Optional string
* `Int` - Integer
* `Float` - Floating point number
* `Boolean` - True/false
* Custom input types

### Example with multiple variables

```graphql theme={null}
query GetDeals($first: Int, $sortDirection: SortDirection) {
  deals(first: $first, sortDirection: $sortDirection) {
    edges {
      node {
        id
        friendlyId
      }
    }
  }
}
```

**Variables:**

```json theme={null}
{
  "first": 10,
  "sortDirection": "DESC"
}
```

## Fragments

Fragments allow you to reuse field selections across multiple queries.

### Basic fragment

```graphql theme={null}
fragment BorrowerFields on Borrower {
  id
  personalInformation {
    firstName
    lastName
    email
  }
}

query GetDeal {
  deal(id: "123") {
    id
    borrowers {
      edges {
        node {
          ...BorrowerFields
        }
      }
    }
  }
}
```

### Multiple fragments

You can use multiple fragments in a single query:

```graphql theme={null}
fragment BorrowerFields on Borrower {
  id
  personalInformation {
    firstName
    lastName
  }
}

fragment IncomeFields on Income {
  statedMonthlyAmount
  payPeriodFrequency
}

query GetDeal {
  deal(id: "123") {
    id
    borrowers {
      edges {
        node {
          ...BorrowerFields
          incomes {
            edges {
              node {
                ...IncomeFields
              }
            }
          }
        }
      }
    }
  }
}
```

### Selecting loan fields

Request the fields you need directly on a loan:

```graphql theme={null}
query GetLoan {
  loan(id: "123") {
    id
    loanPurpose
    purchasePrice
    maxLoanAmount
  }
}
```

### Inline fragments

Use inline fragments to select fields that only exist on a specific implementation of an interface. For example, `institutionName` is only available on assets that implement `FinancialAccountAsset`:

```graphql theme={null}
query GetBorrowerAssets($id: ID!) {
  borrower(id: $id) {
    id
    assets {
      id
      amount
      ... on FinancialAccountAsset {
        institutionName
      }
    }
  }
}
```

## Best practices

1. **Use variables for dynamic values** - Never hardcode IDs or filters
2. **Create reusable fragments** - Define common field selections once
3. **Name fragments descriptively** - Use clear, descriptive names
4. **Keep fragments focused** - Don't create overly large fragments

## Common patterns

### Reusable borrower fragment

```graphql theme={null}
fragment BorrowerDetails on Borrower {
  id
  personalInformation {
    firstName
    lastName
    email
    phoneNumbers {
      number
      type
    }
  }
  incomes {
    edges {
      node {
        statedMonthlyAmount
        payPeriodFrequency
      }
    }
  }
  assets {
    id
    amount
    ... on FinancialAccountAsset {
      institutionName
    }
  }
}
```

### Using in multiple queries

```graphql theme={null}
query GetDeal($id: ID!) {
  deal(id: $id) {
    id
    borrowers {
      edges {
        node {
          ...BorrowerDetails
        }
      }
    }
  }
}

query GetBorrower($id: ID!) {
  borrower(id: $id) {
    ...BorrowerDetails
  }
}
```
