Query Anything by ID

Many objects in the API have unique IDs and implement the Node interface. This lets you fetch any object by its ID using a single query pattern, even if you don't know what type it is.

Why This Matters

When you create a booking or order, you get an ID back. Later, you might need to fetch that booking or order to check its status or retrieve tickets. Instead of remembering different query patterns for each type, you can use the node query for everything.

This is especially useful when:

  • Storing IDs in your database and fetching them later
  • Building generic UI components that work with any object type
  • Fetching objects from webhooks or callbacks where you only have an ID

How It Works

Objects that implement Node have a unique id field. You can fetch any of these objects using the node query by providing the ID:

graphql
query {
  node(id: "some-id-here") {
    # Query fields here
  }
}
query {
  node(id: "some-id-here") {
    # Query fields here
  }
}

Since node can return different types, you need to use inline fragments to specify which type you're expecting and what fields you want.

Using inline fragments

The node query can return any type that implements Node, so you must use inline fragments (... on TypeName) to tell GraphQL what type you expect and what fields to return. See the GraphQL documentation for details.

Examples

Fetching a Booking

If you have a booking ID and want to check its status:

Example: Get booking by ID
graphql
query GetBooking {
  node(id: "e1b3eca6-0d8f-4f95-a584-adda216faa21") {
    ... on Booking {
      id
      selections {
        ... on JourneyOfferSelection {
          offers {
            id
            price {
              amount
              currency
            }
            parts {
              ... on AdmissionPart {
                conditions {
                  description
                  type
                }
                flexibility
                serviceClass
                comfortClass
              }
              ... on ReservationPart {
                conditions {
                  description
                  type
                }
                flexibility
                comfortClass
                accommodation {
                  type
                }
              }
            }
          }
        }
      }
    }
  }
}
query GetBooking {
  node(id: "5091bfcc-a7d6-4297-8ea1-6284a10b7d65") {
    ... on Booking {
      id
      selections {
        ... on JourneyOfferSelection {
          offers {
            id
            price {
              amount
              currency
            }
            parts {
              ... on AdmissionPart {
                conditions {
                  description
                  type
                }
                flexibility
                serviceClass
                comfortClass
              }
              ... on ReservationPart {
                conditions {
                  description
                  type
                }
                flexibility
                comfortClass
                accommodation {
                  type
                }
              }
            }
          }
        }
      }
    }
  }
}

Fetching an Offer

If you stored an offer ID and want to fetch its details:

Example: Get offer by ID
graphql
query GetOffer {
  node(id: "47ce0e60-c4d0-43cf-b697-9f92be3bbf92") {
    ... on Offer {
      id
      price {
        amount
        currency
      }
      parts {
        ... on AdmissionPart {
          flexibility
          serviceClass
          comfortClass
        }
        ... on ReservationPart {
          flexibility
          comfortClass
          accommodation {
            type
          }
        }
      }
    }
  }
}
query GetOffer {
  node(id: "13dd21b9-cf58-47e2-8578-4a0d82bd0cd1") {
    ... on Offer {
      id
      price {
        amount
        currency
      }
      parts {
        ... on AdmissionPart {
          flexibility
          serviceClass
          comfortClass
        }
        ... on ReservationPart {
          flexibility
          comfortClass
          accommodation {
            type
          }
        }
      }
    }
  }
}

Fetching an Order

The most common use case: fetching an order to retrieve tickets or pass codes:

graphql
query GetOrder {
  node(id: "order-id") {
    ... on Order {
      id
      status
      items {
        # Get tickets or pass codes
      }
    }
  }
}
query GetOrder {
  node(id: "order-id") {
    ... on Order {
      id
      status
      items {
        # Get tickets or pass codes
      }
    }
  }
}

Available Node Types

The following types implement the Node interface and can be fetched using the node query:

Best Practices

  1. Store IDs: When you create bookings, orders, or other objects, store the IDs in your database for later retrieval.

  2. Use specific fragments: Always use inline fragments (... on TypeName) to specify the expected type and request only the fields you need.

  3. Handle errors: If an ID doesn't exist or is invalid, the query will return null. Handle this gracefully in your code.

  4. Check types: If you're not sure what type an ID represents, you can query the __typename field to identify it.

Next Steps