kloeckner-i / ember-cli-mirage-graphql

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

Next Steps #34

Open jneurock opened 4 years ago

jneurock commented 4 years ago

I have the idea to separate this addon into multiple libraries by its logical parts. Those being:

My tentative plan is to create a GraphQL auto-resolver library that expects a resolution strategy to be supplied to it and separately a Mirage auto-resolution strategy. The Ember addon would then become a thin wrapper around these two libraries or it would be deprecated.

I see several advantages here:

I've already started experimenting with these ideas. As I spend more time working on the separate libraries the implementation details will become more clear. So far, these are my thoughts:

jgwhite commented 4 years ago

This plan sounds wonderful to me. Some thoughts:

jneurock commented 4 years ago

Thanks for the input!

  • Are there any existing “auto-resolution” libraries that we can take inspiration from?

I tried to find some but I had no luck. That said, I'm more than happy to check out a project if someone finds something.

  • It seems like code-generation is very popular in the GraphQL ecosystem (or maybe just the intersection of Go and GraphQL?) — is this something worth discussing?

I'll be honest, I'm not sure what this means in the context of Mirage. Do you mean generating resolvers, e.g., generating resolvers for mutations? I'm intrigued.

jgwhite commented 4 years ago

Do you mean generating resolvers, e.g., generating resolvers for mutations? I'm intrigued.

Yeah, exactly. Generating a bunch of javascript that implements all the resolvers (for mutations and queries). I don’t really know what the advantage of this is but it does seem to be a popular approach (at least in Go projects).

jneurock commented 4 years ago

Ah, ok. I think it doesn't apply to queries, at least so far, since this addon basically assumes it knows how to fetch anything from Mirage but mutations are another story. This idea might actually be really nice for mutations.

Chad and I were pairing on this addon during the early stages and we actually implemented default mutations. Then we thought about it more and decided mutations were too broad to implement a default. We assumed everyone would basically override our default behavior for most of the mutations.

But... if we could generate a default mutation on demand, this could be really, really nice. For example, at kloeckner.i we have tons of mutations that are basically doing things like: "Create or update a record of type User with an input type called UserInput".

These mutations are super simple and this pattern could be common enough that a generator for this case might be a big time saver.

jneurock commented 4 years ago

I've been looking into this a bit more and have an update...

It seems like I may not need to create 2 separate libraries. graphql-js allows for default resolvers that run whenever there are no resolvers in the schema for a given field or type. The information it supplies to the default resolvers looks like it will be sufficient for getting the right data from Mirage.

I'll experiment more with this approach and see how far I can get. So far, it's looking promising.

Related: In my experimentation I'm dropping usage of mocks for resolvers. I'm also allowing for resolvers to be passed into the options hash. I think this will make the ability to map args and variables to functions redundant.

This approach would also change how mutations are expected in the options. Instead of mutations having their own key in the options, they would be added to the resolvers. I'm thinking backward compatibility should be fine and the old options should be dropped in 1.0.