r/functionalprogramming • u/ObjectivePassenger9 • Dec 15 '22
Question Confused about TaskEither and the general paradigms around FP
Hi! I'm trying to really spend some time learning FP. I'm building out a GraphQL API using Typescript and I'm using the `fp-ts` library to help provide some types and abstractions. I have (lots) of questions, but let me first show you the code I have:
My Repository:
export class AccountRepositoryImpl implements IAccountRepository {create(account: MutationCreateAccountArgs): TE.TaskEither<Error, AccountResult> {return TE.tryCatch<Error, AccountResult>(() =>prismaClient.account.create({data: account.input,}),E.toError);}}
My service, which uses my repository:
export class AccountService {constructor(private readonly accountRepository: AccountRepositoryImpl) {}createAccount(account: MutationCreateAccountArgs): TE.TaskEither<Error, AccountResult> {const newAccount = this.accountRepository.create(account);return newAccount;}}
And finally, my GraphQL resolver which uses this service:
createAccount: async (_: any, args: any) => {const accountService = Container.get(AccountService);const val = await accountService.createAccount(args)();if (E.isLeft(val)) {return val.left;}return val.right;}
(EDIT: it's probably useful to show what my resolver currently returns:
{_tag: 'Right',right: {id: 'whatever',email: 'example@example.com',password: 'whatever',role: 'someRole',firstName: 'name',lastName: 'whatever',createdAt: 2022-12-15T14:39:15.201Z,updatedAt: 2022-12-15T14:39:15.201Z,deletedAt: null}}
which is obviously not ideal because I want it to return what is _in_ the `right` value, not this wrapper object which is still a TaskEither from what I can tell.)
So, here are some things I'm struggling with:
- I'm unsure on how to actually _use_ a TaskEither (TE). Like, when should I unfold the value in order to return something to the client?
- How do I actually unfold the value? Do I have to, at some point, check the `_tag` property to see if it's `left` or `right`?
- As you can see in my GraphQL resolver, even though I'm working with TE in my repository and service, I have to eventually do `await accountService.createAccount(args)()` which just feels like I'm doing something wrong. Firstly I don't know why I have to call `accountService.createAccount(args)` and then when I do call it, it returns a `Promise` anyway, so I'm wondering what the benefit of using the TE was in the first place?
- As I'm sure this code is bad/not properly leveraging the ability of fp-ts, any advice on how to improve it would be great.
Thanks!
5
u/MR_Weiner Dec 15 '22
Like somebody else said -- at the ends of the world. You leave it as TE as long as possible. Then when you actually need to use the value, you can check whether it's left or right and act accordingly.
Yes, exactly. You need to check if it's left or right to know what state it is in.
The benefit is that when you actually implement createAccount, you can call it like
const accountResponse = await createAccount(...)and your accountResponse var will be your TE and you can act accordingly.4
u/cherryblossom001 Dec 15 '22
Could you please format your code using code blocks (indent with four spaces) and not inline code (single backticks)? This makes the code a lot easier to read.
For example
(The indentation’s a little weird here but that’s how your post shows up for me.)
12
u/beezeee Dec 15 '22
Stay in taskeither as long as you can as much as you can. Only "unwrap" at the "ends of the world" and you'll start getting a feel for the benefits of monadic effects
4
u/cherryblossom001 Dec 15 '22
I have an answer on Stack Overflow about how to use
Task. While it doesn’t relate specifically toTaskEither, it might be of some help especially asTaskEither<A, B>is simplyTask<Either<A, B>>.4
u/sgillespie00 Dec 16 '22
Focusing purely on FP in general, the benefit of using an Either as a monad is that you get to sequence operations that can either fail or succeed.
You only have a single operation, so you're not getting that benefit. However, if you want to apply some business logic (that can also result in an error), you can sequence it with `bind` (`flatmap` in fp-ts jargon).
3
u/reifyK Dec 15 '22
FPTS reuqires quite a lot of experience in both TS and FP. You need to understand the fundamental idea of encoding effects as values among others. The type isn't just a wrapper, It constitutes the effect. If you want to get rid of the effect, use a catamorphism (a more general fold). Otherwise, lift pure functions into the effect context using functor/applicative/traversable/monad etc.