Fetching single items

Fetching single items

When you want to fetch an individual content item of a given type you can use the single item query type. As explained in the schema generation docs, the name of the fields is the Singular name of the content model from which they derive.

In the following example, we use a content model called Post, with a Singular name of Post.

query {
  Post( id: "535c1e5a-4d52-4794-9136-71e28f2ce4c1" ) {
    _id,
    title
  }
}

You can make your query more specific by including arguments. A full list of available arguments can be found below.

Arguments

The following arguments are available or required when querying a single content item:

argumenttyperequireddescription
idStringfalseThe ID of the content item you want to fetch
slugStringfalseThe slug of the content item you want to fetch
localeStringfalseLocale for the content item. If not set, the default locale is used.
environment_idStringfalseThe environment ID is required if an organization token is used. It is used to fetch the content item from a specific environment.

Querying by ID

Since IDs are unique in Prepr, you can find the content item you want using the id argument. Here's an example that shows how to query a Post type content item with the id "535c1e-...".

query {
  Post( id: "535c1e-4d52-4794-9136-71e28f2ce4c1" ) {
  _id,
  title
  }
}

Querying by Slug

Since slugs are unique within a content model, you can find a content item you want using the slug argument. Here's an example that shows how to query a Page type content item with the slug "about-us".

query {
  Page( slug: "about-us" ) {
  _id,
  _slug
  title
  }
}

Querying single-item models

Some models only have a single content item. It's possible to query these items without providing an ID because only one item is returned. Here's an example that shows how to query such an item.

query {
  AppSettings {
    _id,
    site_enabled
  }
}

For more details on single-item models, check out the Single-item model docs.

Was this article helpful?

We’d love to learn from your feedback