Skip to content

Commit

Permalink
Handle generating primitive types as responses (#14)
Browse files Browse the repository at this point in the history
* Handle generating primitive types as responses

* Add tests for bool and string primitive type gen

* Update CHANGELOG
  • Loading branch information
youngkidwarrior authored Aug 22, 2024
1 parent 33c7cc6 commit 04d6468
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# master

- Generate response types for primitve return types (bool, string, int, etc).

# 0.7.0

- Depend on ReScript `>=11.1.0` and latest `Core`.
Expand Down
26 changes: 26 additions & 0 deletions cli/EdgeDbGenerator.res
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ module AnalyzeQuery = {
ctx.distinctTypes->Set.add(recordDef)
name
}
and generatePrimitive = (~codec: Codec.t, ~ctx: ctx) => {
let name = Utils.pathToName(ctx.currentPath)
let annotations = if ctx.currentPath->Array.at(0) === Some("args") {
"@live \n"
} else {
""
}
ctx.distinctTypes->Set.add(`${annotations}type ${name} = ${codec->walkCodec(ctx)}`)
}
and walkCodec = (codec: Codec.t, ctx: ctx) => {
open Codec
if codec->is(nullCodec) {
Expand Down Expand Up @@ -341,6 +350,23 @@ module AnalyzeQuery = {
distinctTypes,
},
)->generateSetType(cardinality)
// walkCodec only handles object codec and named tuple codec.
// If distinctTypes is empty, or it is missing a response type,
// then we know we are working with a primitive return type.
if (
distinctTypes->Set.size === 0 ||
!(
distinctTypes
->Set.values
->Iterator.toArrayWithMapper(t => String.includes(t, "response"))
->Array.includes(true)
)
) {
WalkCodec.generatePrimitive(
~codec=outCodec,
~ctx={optionalNulls: false, currentPath: ["response"], distinctTypes},
)
}
Ok({
result,
args,
Expand Down
38 changes: 37 additions & 1 deletion dbTestProject/src/Movies.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,42 @@ let allMovies = client => {
client->query
}

let countAllMovies = client => {
let query = %edgeql(`
# @name countAllMovies
select count(Movie);
`)

client->query
}

let countAllMoviesWithParam = (client, ~title) => {
let query = %edgeql(`
# @name countAllMoviesWithParam
select count(Movie filter .title = <str>$title);
`)

client->query({title: title})
}

let testBool = client => {
let query = %edgeql(`
# @name testBool
select '!' IN {'hello', 'world'};
`)

client->query
}

let testString = client => {
let query = %edgeql(`
# @name testString
select r'A raw \n string';
`)

client->query
}

module Nested = {
let query = %edgeql(`
# @name allMoviesNested
Expand Down Expand Up @@ -58,7 +94,7 @@ let movieByTitle = (client, ~title) => {
json
}
}
}
}
filter .title = <str>$title
limit 1
`)
Expand Down
77 changes: 75 additions & 2 deletions dbTestProject/src/__generated__/Movies__edgeql.res

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions dbTestProject/test/TestProject.test.res
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ describe("fetching data", () => {
expect(movies)->Expect.toMatchSnapshot
})

testAsync("counting movies", async () => {
let count = await client->Movies.countAllMovies
expect(count)->Expect.toMatchSnapshot
})

testAsync("counting movies with param", async () => {
let count = await client->Movies.countAllMoviesWithParam(~title="The Great Adventure")
expect(count)->Expect.toMatchSnapshot
})

testAsync("test bool", async () => {
let bool = await client->Movies.testBool
expect(bool)->Expect.toMatchSnapshot
})

testAsync("test string", async () => {
let string = await client->Movies.testString
expect(string)->Expect.toMatchSnapshot
})

testAsync("fetching single movie", async () => {
let movie = await client->Movies.movieByTitle(~title="The Great Adventure")
let movie = movie->JSON.stringifyAny(~replacer=removeIds, ~space=2)->Option.getOr("")
Expand Down
33 changes: 33 additions & 0 deletions dbTestProject/test/__snapshots__/TestProject.test.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,36 @@ File "Movies.res":
- .id
"
`;

exports[`fetching data counting movies 1`] = `
{
"TAG": "Ok",
"_0": 3,
}
`;

exports[`fetching data counting movies with param 1`] = `
{
"TAG": "Ok",
"_0": 1,
}
`;

exports[`fetching data running in a transaction 1`] = `undefined`;

exports[`fetching data test bool 1`] = `
{
"TAG": "Ok",
"_0": false,
}
`;

exports[`fetching data test string 1`] = `
{
"TAG": "Ok",
"_0":
"A raw
string"
,
}
`;

0 comments on commit 04d6468

Please sign in to comment.