relayjs / eslint-plugin-relay

A plugin for the code linter ESLint to lint specific details about Relay.
MIT License
100 stars 47 forks source link

[unused-fields] Add an ignoredFields option #122

Open azz opened 3 years ago

azz commented 3 years ago

👋 Hey team.

Context

In our codebase, we use connections pretty extensively with graphql-ruby. Because there's a lot of nullability in our graph, we have a library function to extract all the nodes from a connection. For example:

const {bars} = useFragment(graphql`
  fragment Foo_foo on Foo {
    bars {
      edges {
        node {
          name
        }
      }
    }
  }
`, props.foo);

// getNodes will grab all the nodes from `bars` filtering out nulls etc.
getNodes(bars).map(bar => <div>{bar.name}</div>);

As you can imagine, this yields a bunch of unused-field warnings on the generic edges and node fields across a whole bunch of files.

Solution

I'm proposing an option to unused-fields that allows us to suppress warnings for these common fields, like so:

  'relay/unusued-fields': ['warn', {ignoredFields: ['node', 'edges']}]

I've raised a PR with specs and docs. Hope you're happy to 🚢 !

azz commented 3 years ago

Hey, @kassens, sorry to ping direct, but any chance of getting this shipped?

bonham000 commented 2 years ago

Would be great to see a feature like this. We have a codebase where we have a lot of id fields which we need to query but are not used directly in the components. It would be nice to just ignore linting for id fields.

@acconrad @kassens any update here?

muhajirdev commented 2 years ago

in case anyone want to use this feature before this PR got merged. yarn patch eslint-plugin-relay just google yarn patch

rbalicki2 commented 2 years ago

Sorry about the late replies!

Let me know what you think! I'm not opposed to merging something like this, but IMO we should try to fix whatever issue we have with Relay, as it should be pretty rare that you need to select fields you don't use. (Or that eslint isn't smart enough to figure out that you're using.)

michael-haberzettel commented 2 years ago

Relay resolvers are great but they are still experimental even in last Relay version. We have a similar issue in our codebase with edges and node, this could be great if this option can be used.

This PR https://github.com/relayjs/eslint-plugin-relay/pull/95 seems related to same the issue.