kloeckner-i / ember-cli-mirage-graphql

A library for mocking GraphQL with Ember CLI Mirage
MIT License
24 stars 10 forks source link

Mirage models and relationships #26

Open skarger opened 5 years ago

skarger commented 5 years ago

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.

jneurock commented 5 years ago

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?

skarger commented 5 years ago

@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.

dmzza commented 5 years ago

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.

skarger commented 5 years ago

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

jneurock commented 5 years ago

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.

dmzza commented 5 years ago

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.

jneurock commented 5 years ago

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 commented 4 years ago

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