mst-gql icon indicating copy to clipboard operation
mst-gql copied to clipboard

Make primitives easily chainable

Open Aryk opened this issue 5 years ago • 7 comments

My understanding with primitives is that you have to do something like this to actually be able to continue the chain.

rs => rs.id._(postReactionModelPrimitives.__query).user(blahblah)

Wouldn't be cleaner and more flexible to have

const postReactionModelPrimitives = rs => rs.id.foo.bar.car

That way we could do:

rs => postReactionModelPrimitives(rs).user(blahblah)

Or maybe I'm missing something from the documentation?

Aryk avatar Apr 28 '20 19:04 Aryk

I'm not entirely sure what is going on here, what is your use case?

chrisdrackett avatar Apr 29 '20 19:04 chrisdrackett

Nevermind, I figured out how to chain these query selectors, we're all good!

Aryk avatar Apr 30 '20 11:04 Aryk

Actually @chrisdrackett seems like I'm not out of the woods. When it comes to building selectors, it's quite confusing. There are three variations...all of them "work".

const basicUserSelectorA = (new UserModelSelector()).id.fullName.username
const basicUserSelectorB = u => u.id.fullName.username

queryUser({}, rs => rs.result.user(basicUserSelectorA.foo.bar))
queryUser({}, rs => rs.result.user(u => basicUserSelectorB(u).foo.bar)
queryUser({}, rs => rs.result.user(() => basicUserSelectorA.foo.bar))

All three "work", however I found that when I create long queries, with every refresh, my query would keep concatenating on itself and I would end up with an every increasing query.

This would only happen in this variation:

queryUser({}, rs => rs.result.user(basicUserSelectorA.foo.bar))

How exactly is the recommended way to build selectors, A or B?

And then if you need to combine selectors on the same object, how do you do it.

For example, I have want to have two different selectors:

const basicUserSelectorA = (new UserModelSelector()).id.fullName.username
const basicUserSelectorC = (new UserModelSelector()).birthday

I noticed that the "primitives" auto created by mst-gql are build in this way (not the arrow function way as in basicUserSelectorB). How do you combine two together. Do you have to do:

basicUserSelectorA._(basicUserSelectorC)

^^ Like that?

Aryk avatar May 01 '20 19:05 Aryk

@Aryk sounds like what you are running into might be related to https://github.com/mobxjs/mst-gql/issues/206

I don't use these selectors / selector builders myself, so it's hard to me to currently comment on how this currently / should work at the moment without digging deeper.

chrisdrackett avatar May 07 '20 15:05 chrisdrackett

Just curious, do you know if it's required to select the "id" or ID field in the results selector? I've noticed the primitives don't have that included, so I'm wondering how MST GQL knows to assign those attributes to the item in the store with that "id" if the id is not explicitly included.

Aryk avatar Jul 11 '20 20:07 Aryk

I believe that both id and __typename are always added as part of the query builder. We still explicitly add them ourselves, just to be safe.

chrisdrackett avatar Jul 14 '20 17:07 chrisdrackett

Got it, that's what I thought...I pretty sure it does that bc I left it off a few times and things still work.

Aryk avatar Jul 14 '20 17:07 Aryk