query and queryRecord

Search the Backend With Params

query and queryRecord

The query method returns a collection from a parameterized request, queryRecord returns a single matching record.

4 min read Level 2/5 #ember#ember-data#queries
What you'll learn
  • Call store.query with type and params
  • Call store.queryRecord for single-record searches
  • Distinguish query from findAll

When the server expects parameters — filters, sort, pagination, search — use query for collections or queryRecord for a single resource.

query

const recent = await this.store.query('post', {
  sort: '-createdAt',
  filter: { published: true },
  page: { size: 20 },
});

The adapter turns the params object into a URL:

GET /posts?sort=-createdAt&filter[published]=true&page[size]=20

The response populates the store, and the returned array stays in sync if those records change.

queryRecord

For endpoints that return one record, like a “current user” route:

const me = await this.store.queryRecord('user', { current: true });

This hits GET /users?current=true and returns the first record from the response. Use it instead of findRecord when you don’t have an id yet.

query vs findAll

  • findAll('post') — “give me every post.” Hits GET /posts once and uses the cache after that.
  • query('post', params) — “give me posts matching this filter.” Each unique param set is its own request. Results still merge into the same identity map, so a record loaded via query is the same object as via findRecord.

Pagination

A common pattern is page-based queries:

@tracked page = 1;

get posts() {
  return this.store.query('post', {
    page: { number: this.page, size: 10 },
  });
}

Each page is its own request; the records flow through the same store.

Adapters →