GraphQLBundle icon indicating copy to clipboard operation
GraphQLBundle copied to clipboard

Merging of numeric arrays by inheritance

Open murtukov opened this issue 6 years ago • 1 comments

Q A
Bug report? yes
Feature request? no
BC Break report? yes/no
RFC? no
Version/Branch 0.13

Given following decorator:

UserDecorator:
    decorator: true
    config:
        fields:
            username: String!
            password: String!

If I inherit this decorator to add 1 more field and override the type of the field password:

UserInput:
    type: input-object
    inherits: [UserInput]
    config:
        fields:
            password: Int! # overriding the type
            email: String! # adding 1 more field

then I get following result as expected:

# ...
        fields:
            username: String!
            password: Int!
            email: String!

So the existing options are overridden, non-existing options are added.


But if I try to inherit using validation constraints like this:

UserDecorator:
    decorator: true
    config:
        fields:
            username: String
            password:
                type: String!
                validation:
                    - NotNull: ~
                    - Length: {min: 1, max: 10}
UserInput:
    type: input-object
    inherits: [UserInput]
    config:
        fields:
            password:
                type: Int!  # override type
                validation: # modify constraints
                    - Json: ~ # add new constraint
                    - Length: {min: 50, minMessage: "The value is too long"} # override params
            email: String!

then I get some weird result:

# ...
        fields:
            username: String!
            password:
                type: Int!
                validation:
                    - NotNull: ~
                    - Length: {min: 50, minMessage: "The value is too long"}
            email: String!

Expected result:

# ...
        fields:
            username: String!
            password:
                type: Int!
                validation:
                    - Json: ~
                    - NotNull: ~
                    - Length: {min: 50, minMessage: "The value is too long"}
            email: String!

I expect that validation constraints are merged and overriden, but their parameters should NOT be merged recursively, but replaced instead

murtukov avatar Oct 12 '19 01:10 murtukov

The thing is, the configs are currently "generic" and therefore merged by 1 specified rule, not depending on content.

So you will probably need to make merging code aware of specific config (I think this is not a good idea) or have some processing happening before. But that might get tricky (AFAIK) to find such a place.

I mentioned the general issue with the merging here: https://github.com/overblog/GraphQLBundle/issues/543#issuecomment-517632814

The code has been since altered twice. Basically there are two ways to merge numeric arrays:

  • The same way as assoc, whereby each item is overridden by numerical index - [0, 1, 2, 3] + [4, 5, 6] becomes [4, 5, 6, 3], or overridden & appended: [4, 5, 6] + [0, 1, 2, 3] becomes [0, 1, 2, 3]
  • By appending items (merge) - [0, 1, 2, 3] + [4, 5, 6] becomes [0, 1, 2, 3, 4, 5, 6].

Both approaches make sense in different scenarios. Also its important to what '~* expands, based on the extension configuration, since the merging happens afterwards.

akomm avatar Oct 15 '19 08:10 akomm