GraphQLSP icon indicating copy to clipboard operation
GraphQLSP copied to clipboard

fix(graphqlsp): Fix infinite loop conditions when resolving fragments

Open kitten opened this issue 6 months ago • 1 comments

Resolve https://github.com/0no-co/gql.tada/issues/374

Summary

Two loops can occur when resolving fragments:

  • Loops between mutual fragments
  • Loops when resolving an identifier

The first one is mostly to prevent crashes, but is already invalid and will lead to invalid behaviour. This occurs when two references reference each other. If TypeScript happens to still be able to resolve this condition, then we'd infinitely jump between the two fragments. This is easily prevented by keeping a WeakSet of Identifiers.

The second condition — which is more common in valid code — happens in scenarios when resolving an identifier leads back to this same identifier. I'm not 100% sure when this happens, but it's easily prevented.

I separated the fragment resolution into two parts to make the issue stand out more as well.

Set of changes

  • Extract GraphQL Node resolution from unrollFragment
  • Prevent resolving the same identifier twice in a row
  • Prevent unrollFragment from recursing
  • Prevent unrollFragment from processing the same identifier twice

kitten avatar Aug 21 '24 21:08 kitten