-
Notifications
You must be signed in to change notification settings - Fork 10
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
Compatibility with Mirage’s ORM layer? #18
Comments
Thanks for opening this issue! There's no reason not to interface with Mirage's ORM layer. A PR to address this issue would certainly be valuable :) |
@jgwhite, if you want, you can create a test that fails and send it my way. |
@jneurock thanks. The team is digging back into this in the next few weeks so hopefully that’ll force us to contribute some test cases and/or an implementation. |
@jgwhite, it seems like this might only work if you use Mirage models. Do you think that's accurate? In my apps, I don't use any Mirage models, only factories. I don't want to go through the trouble of creating Mirage models since there's a lot of overlap with the GraphQL schema. As a result, I seem to only be able to work with Mirage's DB layer. I would like to continue not using Mirage models so maybe a better approach here would be to examine the differences at the DB layer when using Mirage models and relationships vs. having no Mirage models at all. That or the logic could be split for the two cases. One strategy for working with the ORM layer and a fallback for the DB layer, if needed. Let me know what you think. |
Yes, I think that’s the case.
That makes sense. I know Mirage is able to automatically infer models from Ember Data models. I wonder if a similar approach could be taken with the schema? Perhaps this indicates the need for a new public API in Mirage for registering models?
Yeah, that sounds like a good strategy, assuming that the way Mirage stores the relationship data at the DB layer is stable.
If the above strategy works it would certainly be simpler to maintain. |
Thanks, @jgwhite.
I think this has been stable for a long time but I guess I'll find out...
I think we could create Mirage models from the schema with some work but yeah, how do we make Mirage aware of them? Any thoughts @samselikoff? Is this already possible or would we need a public API? |
Could definitely create models from a schema, and yes I think the ORM is still valuable + should be the way a GraphQL lib interacts with Mirage's data layer. The ORM is what lets you pass models into other models while doing |
Thanks, @samselikoff. Quick example of a blog post type in a GraphQL schema: type Post {
id: ID!
author: User # belongsTo
body: String! # scalar
comments: [Comment] # hasMany
title: String! # scalar
} When this gets parsed into JavaScript, we get all kinds of good info. For example, the parsed schema would tell us the |
I can also make it part of my work on #34 to go ahead and create Mirage models from the schema but is there a way to make them known to Mirage programmatically? I'll try to find out when I get to it. |
The programmatic answer would be to get the models/relationships from graphql into some data structure and then pass them into Mirage's config. Here's what it looks like for a new server: import { Model, hasMany } from 'miragejs'
new Server({
models: {
user: Model.extend({
comments: hasMany()
}),
comment: Model.extend({
user: belongsTo()
})
}
}) but you can "augment" existing mirage server's config by calling let server = new Server();
// later...
server.config({
models: {
user: Model.extend({
comments: hasMany()
}),
comment: Model.extend({
user: belongsTo()
})
}
}) Basic goal would just be to form that models hash using data from graphql schema. If it helps, we can also expose some method on server like |
Thanks for the info! It looks like I can go ahead and try this out: https://github.com/miragejs/miragejs/blob/master/lib/orm/schema.js#L58. |
Exactly, nice find 👍 |
Well, it's been a long time but I started working on the plan proposed here a few weeks ago and wanted to leave a progress update.
Some technical details... The Additionally, I'm able to pretty easily create Mirage models with proper relationships based on a GraphQL schema. The GraphQL schema, as tested so far, is a good stand-in for defining Mirage models explicitly and it could be that the conventional workflow is to avoid the redundancy of defining Mirage models when using GraphQL. All but 4 of the test modules from this add-on are currently passing and 2 of the 4 are mutation tests which rely on user-defined resolvers vs. automatic resolution. Remaining tasks:
Feel free to leave questions about the progress here. Thanks for the patience. |
Awesome. Can't wait to see the work. I mentioned to Rocky in Discord, I'm about to wrap up the REST Tutorial for Mirage, and it would be so great to eventually have a GraphQL version of it that we maintain alongside of it 👍 Will be more than happy to help with the docs/examples side once we get to that point. @jgwhite , has anything changed on your end in terms of how y'all are using Mirage + GraphQL? |
@samselikoff nothing’s changed on our end. Still excited to try out any new ideas in our apps. I know @chadian has been exploring some ideas with GraphQL and Mirage too. |
With our powers combined... |
Hello! Yes, I have been toying with the graphql testing ideas the past few months. I had an initial spike in December that I picked up a few months ago and has since come along quite far. The idea I've been working on has a mirage component for "auto resolving" but also even more broadly provides a suite of tools for bootstrapping a graphql handler or resolver maps for testing (ie: getting automated sinon or logging added quickly). The mirage piece received some updates yesterday (graphql-mocks/graphql-mocks#10) and there's a few other ideas I would like to add later if I have time. I'm currently working on documentation and hoping it'll be ready for people to try in a week or two. |
It's been a long time coming but almost there... miragejs/graphql#1 for anyone that would like to review. |
It seems like this library currently interfaces directly with Mirage’s DB layer, rather than the ORM layer. This causes some compatibility problems for projects that use the ORM layer, particularly in code paths such as
filterByParent
†.Interfacing with the ORM layer would potentially simplify loading of associated records and handling of things like non-standard
inverse
field names.Is there a reason not to interface with the ORM layer?
Would a PR that switches ember-cli-mirage-graphql over to the ORM layer be valuable?‡
†
filterByParent
filters onsomeParentField.id
on objects from the DB. When using the ORM layer, the relationship is stored in the DB assomeParentFieldId
.‡ We’d probably have to take backwards compatibility into account for projects that don’t use the ORM layer yet.
The text was updated successfully, but these errors were encountered: