Are there any examples available for TypeScript?
What are you trying to do? It’s basically the same as js but with types of course.
Or are you just looking for help on how to use the Fauna types? They are not very specific, pretty much everything being Expr or ExprArg
I wrote these packages in typescript.
- https://github.com/ptpaterson/faunadb-graphql-schema-loader
- https://github.com/ptpaterson/faunadb-io-types
- https://github.com/ptpaterson/fauna-fields-list-compiler
faunad-io-types
helps you build validators/decoder for io-ts
and easy integration with fp-ts
. I have code using it but haven’t added it as an example to the repo yet.
Typescript is taking over the JavaScript world by storm according to recent polls (and rightfully so in my humble opinion). I’ve written all my current examples in JavaScript since I wanted people to get into it easily.
I’ll keep it in the back of my mind that if I start a new example in the near future, to write it in TypeScript.
https://github.com/ptpaterson/faunadb-io-types looks cool. Do you have any examples yet of how to use it? Thanks.
Do FaunaDB have Response Type in Typescript? I’m finding it’s redundant and not standardized when trying to make Response type especially with Ref, data, and ts key format in Fauna.
Do you have an example? That would be great. Thank you.
Generally you don’t want to return every field from a GQL query. You should query only what you need in order to keep the network traffic low.
For example here is one of my response types. The number of fields in the response is very low compared to what is in the documents:
export interface FindPublicAndPrivatePiersResponse {
data: {
publicPiersAllUsers: {
data: {
_id: string;
description: {
pierName: string;
};
observatory: {
description: {
observatoryName: string;
};
};
}[];
};
ownedObservatoriesThisUser: {
ownedObservatories: {
data: {
description: {
observatoryName: string;
};
piers: {
data: {
_id: string;
description: {
pierName: string;
};
}[];
};
}[];
};
};
};
}
Here is the query:
findPublicAndPrivatePiers(username: string): Observable<FindPublicAndPrivatePiersResponse> {
const query = `
query {
publicPiersAllUsers: findPiersByPublicFlag(isPublic: true) {
data {
_id
description {
pierName
}
observatory {
description {
observatoryName
}
}
}
}
ownedObservatoriesThisUser: findObserverByUsername(username: "${ username }") {
ownedObservatories {
data {
description {
observatoryName
}
piers {
data {
_id
description {
pierName
}
}
}
}
}
}
}`;
return this.executeQuery(query);
}
As you can see, your query can also return related documents so it is not possible to generalize the Response type, at least this something that Fauna can’t do for you.
Hi @hunp! Thanks for bumping this.
faunadb-graphql-schema-loader
and fauna-fields-list-compiler
should work fine, but the dependencies are indeed quite out of date. There are some dependency-bot alerts to take care of there.
There are some problems with faunadb-io-types
that need to be worked through. It hasn’t been a priority. Most project I’ve worked on haven’t dived that deep into using fp-ts
, maybe grab some Task
s, Either
s and Option
s. I’ve also been working on a personal Rust project where deserializing queries into custom types is quite nice. For me that satisfies my desire for types, but that doesn’t help the TS world much, huh
I will make an effort to update the dependencies and test the first two. I will see if I can figure out what’s wrong with the io-types package and at least document an issue if I cannot resolve it quickly. It looks like the io-types lib has been the most clicked, so it deserves some love.
Ok! even without using your libraries, could you help me understand what I can expect when converting my existing js based fauna project to typescript? Thank you. I’m currently in the process of testing the conversion.
Also got this error when I converted my .js to a .ts
To fix the error, you may need to update your tsconfig.json
to include
{
"compilerOptions": {
"esModuleInterop": true,
},
}
The types available for creating queries are pretty simple. Mostly just the Expr
and ExprArg
types. You will want to focus your typing energy on the result types and types for your business logic.
Just because you provide queries with a type, e.g. client.query<MyResult>(...)
, doesn’t mean you have actually guaranteed that that type is returned. I guess this is not just Typescript advice, but general: test your FQL so you are certain of the shape of results AND errors.
Yes,
"esModuleInterop": true,
was key, thank you.
Hopefully I figured out how to proceed with ts. For example to specify something is a Ref
I used function(fidRef:values.Ref)
where values
is gotten from import { values } from "faunadb";
What is faunadb-io-types
for exactly?
Check out https://duck.brainy.sh !
It converts your schemas into a typescript-typed API automatically.
For examples of it working, take a look at brainyduck/examples at master · zvictor/brainyduck · GitHub