Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I use Runway to fetch related models in a query
    Runway GraphQL Eloquent models Statamic fetching related models Belongs To relationship Has Many relationship API integration


How can I use Runway to fetch related models in a query


Runway allows you to fetch related Eloquent models in your GraphQL queries. Here's an example:

Suppose you have an Eloquent model `Product` that has a "Belongs To" relationship with a `Brand` model. You can fetch the product and its associated brand in a single GraphQL query like this:

graphql
{
  product(id: "2") {
    id
    name
    price
    brand {
      id
      name
      created_at
      updated_at
    }
  }
}

In this example, the `brand` field in the `product` query will fetch the related `Brand` model and return its `id`, `name`, `created_at`, and `updated_at` fields.

Runway automatically handles the mapping between your Eloquent models and the GraphQL schema, so you can access the related models using the field names defined in your Eloquent relationships.

This works for both "Belongs To" and "Has Many" relationships. For example, if a `Product` has many `Images`, you can fetch the product and its associated images like this:

graphql
{
  product(id: "2") {
    id
    name
    price
    images {
      id
      url
      alt_text
    }
  }
}

By leveraging Runway's support for querying related Eloquent models, you can build powerful and flexible GraphQL APIs that seamlessly integrate with your Statamic-powered application's data model.

Citations:
[1] https://runway.duncanmcclean.com/graphql
[2] https://runway.duncanmcclean.com/rest-api
[3] https://statamic.com/addons/rad-pack/runway
[4] https://statamic.dev/graphql
[5] https://statamic.dev/rest-api