That’s fair, we are working hard to improve that. In the meantime there is an excellent series from which you can learn FQL step by step: Getting started with FQL, Fauna’s native query language - part 1, e.g. the fourth article dives into conditional logic by verifying whether the spaceship has enough fuel to warp to a planet: Getting started with FQL, Fauna’s native query language - part 4.
I also wrote a complete app with a github repository which also has quite some conditional logic: Rethinking Twitter as a Serverless App | CSS-Tricks - CSS-Tricks
You are thinking in the wrong direction imo. Everything in FaunaDB is a transaction which means that if the Create fails, the whole transaction would fail. That means you can simply continue with your login once you created the user since if the code got that far, the create succeeded. The login won’t happen if the transaction fails. If you do want to check specifics such as whether a user with that email already exists (although you could just use a unique index and let it fail) and do some logic etc you could always try to search it with an index and use the ‘Exists()’ function in combination with the If() function. That said, I’m assuming for what you are trying to do that you don’t need that.
To login after creating the user, you will need to know which user to login with, which means you need to store that user result from the create (or at least the reference) in a variable. For that, Let() is your friend.
Let(
{
user: Create(Collection('users'), {
data: { email: Select('email', Var('input')) },
credentials: { password: Select('password', Var('input')) }
}),
// you could add more variables here that use the previous variables e.g.
token: Login(Select(['ref'], Var('user')), Select('password', Var('input')))
},
// and then return it.
{
token: Var('token'),
user: Var('user')
}
)
You could also just create the token since you already know that the password is correct, you just created the user.
Let(
{
user: Create(Collection('users'), {
data: { email: Select('email', Var('input')) },
credentials: { password: Select('password', Var('input')) }
}),
// you could add more variables here that use the previous variables e.g.
token: Create(Tokens(), { instance: Select(['ref'], Var('user')) })
},
// and then return it.
{
token: Var('token'),
user: Var('user')
}
)
Note that I didn’t test the code, there might be syntax errors.
I do think you already found this out right? We definitely need more guidance there. In your case. You will have to be careful that the result of your query closely matches the result that GraphQL expects which is probably the hardest part.
There are few basic examples here but we definitely need something more advanced:
Tips:
- make sure to define your schema to return the types you intend to return from your UDF.
- make sure not to return FQL formats such as ‘Ref’, take the id out of there.
- when in doubt, look at another GraphQL result carefully and try to see the difference with the FQL result (you could just test by calling your UDF via the dashboard console).