ember-cli-mirage-graphql icon indicating copy to clipboard operation
ember-cli-mirage-graphql copied to clipboard

Mirage models and relationships

Open skarger opened this issue 5 years ago • 8 comments

I'm using the currently released ember-cli-mirage-graphql version, 0.1.1.

This video guide about ember-cli-mirage-graphql uses Mirage factories, and so does this repo's dummy app.

Mirage also provides the concepts of models and relationships. Specifically, you can define belongsTo and hasMany relationships between models.

  1. Does this package work with those Mirage features?
  2. If so, would it be possible to add an example, maybe to the dummy app, of how to handle a GraphQL query that loads associated models from the Mirage DB?

I tried using Mirage models in my app, but when handling the GraphQL query it seemed to have trouble loading the related models. I'm not sure if I was lacking some necessary configuration or it's just not supported.

By the way, this may be tangentially related to #18.

skarger avatar Oct 17 '19 00:10 skarger

My first thought is that the issue is related to #18, as you mentioned. There are some related records in the tests but I think the add-on currently expects them to be associated in a certain way, which isn't very flexible.

Here is an example: https://github.com/kloeckner-i/ember-cli-mirage-graphql/blob/master/tests/acceptance/customer-test.js#L11. Are you able to relate your records like this for the time being?

jneurock avatar Oct 17 '19 04:10 jneurock

@jneurock OK, looks like that example uses the customer and order factories to create a relationship, rather than Mirage hasMany / belongsTo relationships. That will potentially work for me, I'll give it a try.

skarger avatar Oct 17 '19 14:10 skarger

I watched the same video guide. I’ve been trying to get hasMany relationships to work. One-to-one belongsTo relationships work fine. I’m using factories for every model in my default Scenario.

@skarger Have you been able to get a hasMany relationship working in development (not just in tests)?

I’m thinking of adding a default Scenario to the dummy app to see if it works in development.

dmzza avatar Oct 18 '19 19:10 dmzza

@dmzza no, but I haven't tried yet.

skarger avatar Oct 18 '19 20:10 skarger

If you'd like, feel free to fork and add a failing test, or add an example to the dummy app that doesn't work, so I can see where the addon is breaking down.

jneurock avatar Oct 18 '19 21:10 jneurock

After a lot of exploration, I found the most significant cause of the problem for me. I'm transitioning from ember-data to GraphQL, and I didn't want to delete my ember-data models yet. So Mirage was reading my models and that broke all of my hasMany relationships.

To fix that, I added this to config/environment.js:

    ENV['ember-cli-mirage'] = {
      discoverEmberDataModels: false
    };

Now I can keep all of my models so that my typescript still compiles while I make the transition from ember-data to GraphQL.

However, I also found some inconsistent behavior with the way I construct my relationships using factories. I'm still investigating this, and if I can produce the same problem in the sample app, I might open a PR.

dmzza avatar Oct 20 '19 22:10 dmzza

Ah, very interesting. Thanks for the info. Feel free to open a PR. If you think something should be added to the README to help others with the same situation, feel free to add something there, too.

jneurock avatar Oct 21 '19 07:10 jneurock

This addon will be deprecated in favor of https://github.com/miragejs/graphql which does use Mirage models.

jneurock avatar Jul 05 '20 20:07 jneurock