Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track unused fields #146

Merged
merged 11 commits into from
Dec 21, 2023
Merged

feat: track unused fields #146

merged 11 commits into from
Dec 21, 2023

Conversation

JoviDeCroock
Copy link
Member

@JoviDeCroock JoviDeCroock commented Dec 19, 2023

Resolves #143

Summary

When we are dealing with co-located fragments and operations it can be hard to determine when a field isn't used, especially as your component grows. This tries to prevent that by for queries and fragments implementing basic tracking of a result obtained from either of the following ways

  • const data = useFragment() || const data = await client.query()
  • const [result] = useQuery() --> urql
  • const { data } = useQuery() --> Apollo-client
  • const { data: { pokemon } } = useQuery();
  • const { name } = useFragment()
  • const [{ data }] = useQuery()

I still need to add tests for this whole ordeal and clean up the code as we can probably simplify this by combining both the crawling steps

When people are missing a fragment in a sub-component like

const Q = graphql(`query { pokemons { id name fleeRate } }`)

const [result] = useQUery(Q)

return result.data.pokemons.map(x => <Pokemon key={x.id} pokemon={x} />

Then it will show name and fleeRate as unused as we expect this to be part of the fragment exported by Pokemon.

Notes

We don't track mutations/subscriptions due to how they might be getting used in the normalised cache updaters/...


We don't resolve fragments as we make the assumption that those come from external components so we won't tell you that a fragment/... is unused

Copy link

changeset-bot bot commented Dec 19, 2023

🦋 Changeset detected

Latest commit: a07d830

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@0no-co/graphqlsp Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@JoviDeCroock JoviDeCroock force-pushed the wip-fields branch 9 times, most recently from 4f1aaab to 7e0b809 Compare December 20, 2023 17:03
@JoviDeCroock JoviDeCroock marked this pull request as ready for review December 20, 2023 17:03
@JoviDeCroock JoviDeCroock requested a review from kitten December 20, 2023 17:04
@@ -40,6 +42,7 @@ export const SEMANTIC_DIAGNOSTIC_CODE = 52001;
export const MISSING_OPERATION_NAME_CODE = 52002;
export const MISSING_FRAGMENT_CODE = 52003;
export const USING_DEPRECATED_FIELD_CODE = 52004;
export const UNUSED_FIELD_CODE = 52005;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why do these start at 52k? (just curious)

Copy link
Member Author

@JoviDeCroock JoviDeCroock Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason, hoping for no clashes 😅 if tsserver ever reliably allows us to call into the code-fixes we could use these to i.e. add the missing fragment import, remove the unused fields, replace the deprecated field if we find a use instead, ...

@JoviDeCroock JoviDeCroock merged commit a2180af into main Dec 21, 2023
1 check passed
@JoviDeCroock JoviDeCroock deleted the wip-fields branch December 21, 2023 16:25
@github-actions github-actions bot mentioned this pull request Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warn about unused fields
3 participants