GraphQL is an awesome way to fetch just the right information you need from your backend. It's commonly used in 'headless' environments.
Statamic includes a read-only GraphQL API out of the box. Runway extends upon this so you can query your Eloquent models.
GraphQL must be enabled for each of the resources you wish to query. It's as simple as adding to your config:
// config/runway.php'resources' => [\App\Models\Product::class => ['name' => 'Products','blueprint' => 'product','graphql' => true,],],
// config/runway.php'resources' => [\App\Models\Product::class => ['name' => 'Products','blueprint' => 'product','graphql' => true,],],
You must also ensure you have GraphQL enabled in Statamic as well for it to be available to you.
For each resource, there's two kinds of queries you can do. An 'index' query and a 'show' query:
Example of an index query:
{products {data {idnamepricedescription}}}
{products {data {idnamepricedescription}}}
An index query also allows for pagination between results, you can read up more on that in the Statamic Documentation.
Example of a show query:
{products(id: "2") {idnamepricedescription}}
{products(id: "2") {idnamepricedescription}}
If you're using the 'Belongs To' or 'Has Many' fieldtypes provided by Runway, you can also query the related models.
{product(id: "2") {idnamebrand {idnamecreated_atupdated_at}}}
{product(id: "2") {idnamebrand {idnamecreated_atupdated_at}}}
Notice that in the above example, we have a 'Belongs To' fieldtype which we're querying as simply brand
, instead of brand_id
. Runway removes the _id
for you so you can build a nice, clean query.
You may also filter & sort results the same way you would with the built-in queries. Review the Statamic Docs.