fauna / faunadb-js

Javascript driver for Fauna v4 (deprecated)
https://docs.fauna.com/fauna/v4/
Other
702 stars 76 forks source link

4.7.0 to 4.7.1 causes InvalidValue error when importing code #666

Open carlpeaslee opened 1 year ago

carlpeaslee commented 1 year ago

I get the following error after upgrading from 4.7.0 to 4.7.1. The actual occurs when a test imports code that defines FQL that includes variables which reference other FQL. In this case claim_links_id_unique.ref = Index('claim_links_id_unique').

This code runs fine (and actually isn't even executed by the test that fails since it's defining a UDF). My guess is that faunadb is doing some kind of pre-run static analysis and for whatever reason, because of the order of imports, claim_links_id_unique.ref is undefined at the time faunadb does its analysis?

    InvalidValue: Expected value, but found 'undefined'. Argument 0 for Match is required.

      36 |                     Select(
      37 |                         ['data', 0],
    > 38 |                         Paginate(Match(claim_links_id_unique.ref, Var('claim_link_id')))
         |                                  ^
      39 |                     )
      40 |                 )
      41 |             ),

      at arity (../../node_modules/faunadb/src/query.js:2976:15)
      at Function.Object.<anonymous>.arity.min (../../node_modules/faunadb/src/query.js:2991:3)
      at Match (../../node_modules/faunadb/src/query.js:1084:9)
      at Object.<anonymous> (../../pkgs/db_management/functions/claim_links/get_user_claim_link.ts:38:34)
      at Object.<anonymous> (../../pkgs/db_management/functions/claim_links/get_claim_link.ts:8:1)

Also apologies, I didn't see a template for filing issues so let me know if there is additional information you'd like.

cc @thomas-franceschi

github-actions[bot] commented 1 year ago

Internal ticket number is FE-2847

henryfauna commented 1 year ago

Hi @carlpeaslee, thanks for bringing this up! I'll take a look and get back to you.

henryfauna commented 1 year ago

@carlpeaslee Are you able to share code?

mwilde345 commented 1 year ago

@carlpeaslee this patch was motivated by the decision to validate that Client query arguments match existing typescript types. This just changes where the query gets denied, in the driver itself instead of as a 400 response from the database.

In your case it's exposing the fact that your ExprArg value (claim_links_id_unique.ref) is undefined at the moment your query is submitted to the driver. It doesn't make it to faunadb. Also this javascript driver doesn't do any static analysis of your query. The FQL query function is not "lazy loaded", it's simply executed immediately as it's evaluated in your file during runtime. So it's up to you to ensure that claim_links_id_unique exists before you define this query that uses its ref.

I'm not sure exactly how this would look without knowing more of your code and test framework. But it's a matter of waiting for one fauna query to finish before defining another query that depends on the first query's completion.

For now we want to leave this change in as a patch, because it matches existing typescript definitions. Depending on your and other's feedback we would consider making this a major version change.

carlpeaslee commented 1 year ago

thanks for the helpful responses. I will try to get back to you all soon with more code.