Defining UDFs in VSCode using Typescript

I would like to construct my FQL queries in VSCode for the reasons stated in other posts on this issue. I apologize for the question to extend beyond Fauna specifically, however, the issue is one of cross platform compatibility and integration so this is unavoidable to a large degree (and anyway applicable to all the technology domains taken discreetly).

I found a potentially useful tutorial here.

I have an issue with that tutorial that I have posted on GitHub which is preventing me from simply following the tutorial.

However, my main objective, currently, is to better understand the relationship between typescript and how to more easily construct FQL functions in VSCode which is why I am posting these questions here.

Given that DetermineResult.ts is a ‘pure’ typescript file, am I then effectively writing a significant portion of my FQL queries in typescript? That’s fine if I am, however, attempting to incorporate this in my project (or even just the tutorial) raises further issues:

When I copy/paste (because the VSCode terminal upload doesn’t currently work) the ‘Query’ part of this FQL from the tutorial into the Fauna Dashboard ‘play’ function definition my function FQL becomes:

  name: "play",
  role: null,
  body: Query(
          randomCharacter: RandomString(1, "RPS"),
          opponentSelection: Switch(Var("randomCharacter"), selectionMap)
        DetermineResult(Var("selection"), Var("opponentSelection"))

However, I then get:

RandomString is not defined.
In Fauna Dashboard.
RandomString is imported in the Play.ts file:

import { RandomString, Switch } from "faunadb-fql-lib";

If functions like RandomString from “faunadb-fql-lib” are not recognized by Fauna then how can they be used in the typescript files?

If I resolve this question I suspect I will face a similar issue in relation to DetermineResult

The tutorial states:

“This is our UDF. Note that it references the DetermineResult helper function we created above.”

How? Perhaps I am not understanding well enough how node packages can be used in a context like this?

My questions from above (listed for reference):

  1. Am I then effectively writing a significant portion of my FQL queries in typescript, or is that the wrong way to think about this?
  2. If functions like RandomString from “faunadb-fql-lib” are not recognized by Fauna then how can they be used in the typescript files?
  3. How is DetermineResult referenced? Is it managed somehow by the package?

Thanks …

Hello @FreeRoss !

  1. Writing FQL in VSCode & Typescript is working fine if you call those functions using the JS Client. You can even you Js functions in those FQL queries :slight_smile: But as soon as you will try to call them in the Fauna Dashboard, it won’t work because it expects pure FQL & not a hybrid.

  2. The faunadb-fql-lib lib is using those hybrid FQL queries, so they are not supported into the Dashboard. But using the JS Client will work ! You could also try to create them as UDF.

  3. There is no DetermineResult in Fauna. But just like you quoted, it is an UDF, so you need to create this function in the Fauna Dashboard, then call it using : Call(Function('DetermineResult'), [...args])

Basically, you’re mixing JS Client & Dashboard, the first one allows you to be more flexible & use JS, imported FQL, etc… The Dashboard is only pure FQL (If I’m not wrong) :slight_smile:

// Edit : If you want to be able to call those functions into your Fauna Dashboard, create UDFs for each faunadb-fql-lib functions you’re using & call them like in the 3rd answer above. Depending on the function, the translation to UDF is doable.

1 Like

I resolved the tutorial issue with the fauna-gql-upload package and have been able to create functions, indexes and roles from within VSCode, which is very helpful. I also managed to successfully import and use faunadb-fql-lib functions and ‘DetermineResult’. (which fauna automatically interpreted without my needing to create the function in the dashboard).
Thanks for your response which helped me to clarify and work through the issues …