Skip to content

Generated xxxPrimitives query fragments are not immutable #206

Open
@godness84

Description

@godness84

For each model a xxxPrimitives instance is generated in order to quickly select all the simple fields of a model. It's an instance of QueryBuilder, so you can keep calling its methods to append other complex fields.

Obviously (but it was not so obvious for me at first) if you call methods on your xxxPrimitives instance, whenever you use the same xxxPrimitives instance in other parts of your code then you are going to use the mutated instance, not the original one with just the simple fields.

But in many cases it's very handy to describe the fields that you want as "all the primitive fields plus...". Moreover if you add new fields to your graphql schema and run then generator then your xxxPrimitives instance already catches the fields that you've just added.

I think there are 3 ways of solving this:

  1. xxxPrimitives instance should be an immutable one. All of its methods should return a new instance.
  2. xxxPrimitives should be a factory function that creates a new QueryBuilder every time.
  3. an easy way of cloning a QueryBuilder.

Let's take the 3-twitter-clone example. We have:

export function selectFromMessage() {
  return new MessageModelSelector()
}
export const messageModelPrimitives = selectFromMessage().timestamp.text

In order to clone messageModelPrimitives I think we should write

const clonedMessageModelPrimitives = selectFromMessage()._(messageModelPrimitives.toString())

It's not that beautiful and moreover I should do it for each and every model I have.

I propose solution number 2.
To make it backward compatible, we can name it differently. Taking the example above it would be:

export function createMessageModelPrimitives() {
  return selectFromMessage().timestamp.text
}

Is there a meaning in what I say, or am I completely wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions