dimaMachina / graphql-eslint

ESLint parser, plugin and set rules for GraphQL (for schema and operations). Easily customizable with custom rules. Integrates with IDEs and modern GraphQL tools.
https://the-guild.dev/graphql/eslint
MIT License
778 stars 102 forks source link

Support the fragment spread group when defining alphabetize rule's groups #2291

Open yoavsion opened 1 month ago

yoavsion commented 1 month ago

Is your feature request related to a problem? Please describe.

I'd like to be able to use the alphabetize graphql-eslint rule, and keep all fragment spreads grouped together, preferably at the end of the selection set.

Describe the solution you'd like

The rule already supports the groups option, which supports any literal string or the '*' string for "everything else." I'd like to be able to configure the rule this way:

'@graphql-eslint/alphabetize': [
  'error',
  {
    groups: ['id', '*', '...'],
    selections: ['OperationDefinition', 'FragmentDefinition'],
  },
]

So that this selection set:

{
  updatedAt
  ...SomeFragmentB
  ...SomeFragmentA
  id
}

Would end up being alphabetized this way:

{
  id
  updatedAt
  ...SomeFragmentA
  ...SomeFragmentB
}

Since this line uses the node's name, the ... part of the spread is gone from the lexical comparison, which results in fragment spreads being sorted as if they were just regular field selections:

{
  id
  ...SomeFragmentB
  ...SomeFragmentA
  updatedAt
}

Describe alternatives you've considered

I've considered supporting regular expressions in the groups option, but that seemed like an overkill, even before I realized that ...SomeFragment is anyways treated as SomeFragment.

Additional context Not much to add. I did code up a solution locally, mostly to understand how it ended up sorting items the way it did, but I'm waiting for a discussion on this issue to conclude before moving forward.

yoavsion commented 3 weeks ago

@dimaMachina, is there anything additional I could provide here to help drive the discussion forward? I'd love to hear your thoughts on this and the proposed solution. šŸ™šŸ»

yoavsion commented 2 weeks ago

@dotansimha, sorry for bugging you here šŸ™‡šŸ» ā€”Ā I noticed you're one of the maintainers of this repo, and thought I might reach out.

I'm not sure what the recommended path forward here is, and would love your advice or help with this issue and its proposed solution. I'd love to be able to contribute, and appreciate your help!