Skip to content

Schema type and JSON:API data type mismatch #10491

Description

@musaffa

We get the following error with Ember Data models and WarpDrive schemas:

Missing Resource Type: received resource data with a type 'pages' but no schema could be found with that name.

Schema/Resource type is singular. So there's a mismatch with JSON:API data type which is pluralized by convention in Ember Data JSON:API serializer.

{
  data: [
    {
      type: 'pages',
      id: '1',
      attributes: ...
    }
  ]
}

So the following this.store.request fails.

// route
import Route from '@ember/routing/route';
import { service } from '@ember/service';
import { query } from '@ember-data/json-api/request';
// import { query } from '@warp-drive/utilities/json-api'; also doesn't work

export default class PageIndexRoute extends Route {
  @service store;

  model(params) {
    return this.store.request(query('page', params));
  }
}

Data Model Setup:

// model
import Model, { attr } from '@ember-data/model';

export default class Page extends Model {
  @attr() title;
  @attr() slug;
  @attr() body;
}
// store.js
import { useLegacyStore } from '@warp-drive/legacy';
import { JSONAPICache } from '@warp-drive/json-api';

const Store = useLegacyStore({
  legacyRequests: true,
  cache: JSONAPICache,
  schemas: [],
  handlers: []
});

export default Store;

The same error occurs if we replace Data model with Schema:

// page schema
import { withDefaults } from '@warp-drive/legacy/model/migration-support';

export const PageSchema = withDefaults({
  type: 'page',
  fields: [
    { name: 'title', kind: 'attribute', type: 'string' },
    { name: 'slug', kind: 'attribute', type: 'string' },
    { name: 'body', kind: 'attribute', type: 'string' }
  ]
});
// store.js
import { useLegacyStore } from '@warp-drive/legacy';
import { JSONAPICache } from '@warp-drive/json-api';
import { PageSchema } from 'web-app/data/page/schema';

const Store = useLegacyStore({
  legacyRequests: true,
  cache: JSONAPICache,
  schemas: [
    PageSchema
  ],
  handlers: []
});

export default Store;

Versions:

@warp-drive/ember: 5.8.1
ember-data: 5.8.1

Expectation

WarpDrive should give us a mechanism to use singular type in schema and pluralized type in JSON:API data and translate the type to and from seamlessly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions