Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relationships not yielding results #284

Closed
dugajean opened this issue Jan 19, 2020 · 4 comments
Closed

Relationships not yielding results #284

dugajean opened this issue Jan 19, 2020 · 4 comments

Comments

@dugajean
Copy link

dugajean commented Jan 19, 2020

I have the following fake API:

import { Server, Model, Factory, belongsTo, hasMany } from 'miragejs'
import faker from 'faker'

new Server({
  models: {
    company: Model.extend({ jobs: hasMany() }),
    job: Model.extend({ company: belongsTo() })
  },

  routes() {
    this.namespace = 'api'

    this.get('/companies')
    this.get('/companies/:id')

    this.get('/jobs')
    this.get('/jobs/:id')
  },

  factories: {
    company: Factory.extend({
      name: faker.company.name,
      logo: faker.company.logo,
    }),

    job: Factory.extend({
      title: () => faker.name.jobTitle(),
      createdAt: () => faker.date.past(),
      location: () => `${faker.address.city()}, ${faker.address.country()}`,
      body: () => `${faker.lorem.paragraphs(3)}
- ${faker.lorem.sentence()}
- ${faker.lorem.sentence()}
- ${faker.lorem.sentence()}
- ${faker.lorem.sentence()}
- ${faker.lorem.sentence()}

${faker.lorem.paragraphs(2)}
      `
    })
  },

  seeds(server) {
    server.createList('company', 5).forEach(company => server.createList('job', 10, { company }))
  }
})

When I do GET /api/companies I get one result back: { id: "1" }. I'm expecting 5 companies back. Also when doing GET /api/job/1?include=company I never get the company back in the result, but I get the rest of the fake information.

Any idea what's going on here with my company model? It's simply not working.

@samselikoff
Copy link
Contributor

First, it looks you're mocking out a JSON:API backend so I would use the JSONAPISerializer:

import { JSONAPISerializer } from 'miragejs'

new Server({
  serializers: {
    application: JSONAPISerializer
  }
})

Second, the response to /api/companies does seem wrong... first step in debugging these is to do a server.db.dump() right before your API call, and make sure the database looks as you expect. (Sometimes I do window.server = server to make this easy). Let me know what it says and I can help from there!

@dugajean
Copy link
Author

dugajean commented Jan 20, 2020

@samselikoff Thanks on the advice for adding the serializer; That helped! Are there plans for a JSON-LD serializer in the future?

For the information you requested, this is the output (the companies part):

companies: Array(5)
  0: {jobIds: Array(10), logo: undefined, name: undefined, id: "1"}
  1: {jobIds: Array(10), logo: undefined, name: undefined, id: "2"}
  2: {jobIds: Array(10), logo: undefined, name: undefined, id: "3"}
  3: {jobIds: Array(10), logo: undefined, name: undefined, id: "4"}
  4: {jobIds: Array(10), logo: undefined, name: undefined, id: "5"}

@dugajean
Copy link
Author

I was using the faker.js API incorrectly. It's companyName, not name. Thanks for the help!

@samselikoff
Copy link
Contributor

Awesome, glad you found it!

Haven't heard of JSON LD but if it's popular enough we can add it. For now if I was building a JSON LD app I'd probably make my own application serializer by extending the base Serializer or RestSerializer class, or even JSONAPISerializer (or copying it + changing what I need).

You can take a look at the source and see if you'd like to do that:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants