Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: edgeql structure generation #168

Open
wants to merge 18 commits into
base: edgeql-generation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
"apps/*/test/",
"apps/*/web/"
],
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
excluded: [
~r"/_build/",
~r"/deps/",
~r"/node_modules/",
~r"/test/codegen/queries/",
~r"/test/support/scripts/"
]
},
#
# Load and configure plugins here:
Expand Down Expand Up @@ -131,7 +137,7 @@
{Credo.Check.Refactor.MatchInCondition, []},
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
{Credo.Check.Refactor.Nesting, []},
{Credo.Check.Refactor.Nesting, [max_nesting: 3]},
{Credo.Check.Refactor.UnlessWithElse, []},
{Credo.Check.Refactor.WithClauses, []},

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ jobs:
run: |
EDGEDB_VERSION=${EDGEDB_VERSION%%.*}

for migration_file in `ls priv/edgedb/schema/migrations/`
for migration_file in `ls test/support/schema/migrations/`
do
migration_number=${migration_file%.*}
if test ${migration_number} -gt ${EDGEDB_VERSION}
then
rm "priv/edgedb/schema/migrations/${migration_file}"
rm "test/support/schema/migrations/${migration_file}"
fi
done

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ edgedb-*.tar
# dialyzer
priv/plts/

examples/
test/codegen/queries/
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- rendering hints for query errors from EdgeDB.
- support for generating Elixir modules from EdgeQL queries via `mix edgedb.generate`.
- abitility to pass atoms as valid arguments for enums.

### Changed
- `jason` to be required library, but still configurable.
- `EdgeDB.NamedTuple.to_map/2` to include indexes as keys into result map.

## [0.6.1] - 2023-07-07

Expand All @@ -20,6 +26,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- support for `Elixir v1.15` and `Erlang/OTP 26`.
- support for generating Elixir modules from EdgeQL queries via `mix edgedb.generate`.
- abitility to pass atoms as valid arguments for enums.

### Changed
- `jason` to be required library, but still configurable.
- `EdgeDB.NamedTuple.to_map/2` to include indexes as keys into result map.
Comment on lines +29 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already in the Unreleased section, probably an artifact after a merge conflict


### Fixed

Expand Down
15 changes: 15 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import Config

import_config("#{Mix.env()}.exs")

config :edgedb,
generation: [
queries_path: "test/support/codegen/edgeql/",
output_path: "test/codegen/queries/",
module_prefix: Tests.Codegen.Queries
]

# TODO: clean edgedb/edgeql, edgedb/schema, edgedb.toml
# config :edgedb,
# generation: [
# queries_path: "priv/edgedb/edgeql/",
# output_path: "priv/edgedb/codegen/queries/",
# module_prefix: Tests.Codegen.Queries
# ]
3 changes: 2 additions & 1 deletion edgedb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
server-version = "3.0"

[project]
schema-dir = "./priv/edgedb/schema"
schema-dir = "./test/support/schema"
#schema-dir = "./priv/edgedb/schema"
39 changes: 23 additions & 16 deletions lib/edgedb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ defmodule EdgeDB do
"""
@type result() :: EdgeDB.Set.t() | term()

@typedoc """
Parameter types acceptable by `EdgeDB.query*/4` functions.
"""
@type params() :: map() | list() | Keyword.t()

@doc """
Creates a pool of EdgeDB connections linked to the current process.

Expand Down Expand Up @@ -307,7 +312,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query(client(), String.t(), list() | Keyword.t(), list(query_option())) ::
@spec query(client(), String.t(), params(), list(query_option())) ::
{:ok, result()}
| {:error, Exception.t()}
def query(client, statement, params \\ [], opts \\ []) do
Expand All @@ -317,7 +322,8 @@ defmodule EdgeDB do
output_format: Keyword.get(opts, :output_format, :binary),
required: Keyword.get(opts, :required, false),
is_script: Keyword.get(opts, :script, false),
params: params
params: params,
__file__: opts[:__file__]
}

parse_execute_query(client, q, q.params, opts)
Expand All @@ -332,7 +338,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query!(client(), String.t(), list(), list(query_option())) :: result()
@spec query!(client(), String.t(), params(), list(query_option())) :: result()
def query!(client, statement, params \\ [], opts \\ []) do
client
|> query(statement, params, opts)
Expand All @@ -347,7 +353,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_single(client(), String.t(), list(), list(query_option())) ::
@spec query_single(client(), String.t(), params(), list(query_option())) ::
{:ok, result()}
| {:error, Exception.t()}
def query_single(client, statement, params \\ [], opts \\ []) do
Expand All @@ -363,7 +369,8 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_single!(client(), String.t(), list(), list(query_option())) :: result()
@spec query_single!(client(), String.t(), params(), list(query_option())) ::
result()
def query_single!(client, statement, params \\ [], opts \\ []) do
client
|> query_single(statement, params, opts)
Expand All @@ -378,7 +385,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_required_single(client(), String.t(), list(), list(query_option())) ::
@spec query_required_single(client(), String.t(), params(), list(query_option())) ::
{:ok, result()}
| {:error, Exception.t()}
def query_required_single(client, statement, params \\ [], opts \\ []) do
Expand All @@ -394,7 +401,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_required_single!(client(), String.t(), list(), list(query_option())) :: result()
@spec query_required_single!(client(), String.t(), params(), list(query_option())) :: result()
def query_required_single!(client, statement, params \\ [], opts \\ []) do
client
|> query_required_single(statement, params, opts)
Expand All @@ -409,7 +416,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_json(client(), String.t(), list(), list(query_option())) ::
@spec query_json(client(), String.t(), params(), list(query_option())) ::
{:ok, result()}
| {:error, Exception.t()}
def query_json(client, statement, params \\ [], opts \\ []) do
Expand All @@ -425,7 +432,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_json!(client(), String.t(), list(), list(query_option())) :: result()
@spec query_json!(client(), String.t(), params(), list(query_option())) :: result()
def query_json!(client, statement, params \\ [], opts \\ []) do
client
|> query_json(statement, params, opts)
Expand All @@ -440,7 +447,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_single_json(client(), String.t(), list(), list(query_option())) ::
@spec query_single_json(client(), String.t(), params(), list(query_option())) ::
{:ok, result()}
| {:error, Exception.t()}
def query_single_json(client, statement, params \\ [], opts \\ []) do
Expand All @@ -456,7 +463,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_single_json!(client(), String.t(), list(), list(query_option())) :: result()
@spec query_single_json!(client(), String.t(), params(), list(query_option())) :: result()
def query_single_json!(client, statement, params \\ [], opts \\ []) do
client
|> query_single_json(statement, params, opts)
Expand All @@ -471,7 +478,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_required_single_json(client(), String.t(), list(), list(query_option())) ::
@spec query_required_single_json(client(), String.t(), params(), list(query_option())) ::
{:ok, result()}
| {:error, Exception.t()}
def query_required_single_json(client, statement, params \\ [], opts \\ []) do
Expand All @@ -487,7 +494,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec query_required_single_json!(client(), String.t(), list(), list(query_option())) ::
@spec query_required_single_json!(client(), String.t(), params(), list(query_option())) ::
result()
def query_required_single_json!(client, statement, params \\ [], opts \\ []) do
client
Expand All @@ -500,7 +507,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec execute(client(), String.t(), list(), list(query_option())) ::
@spec execute(client(), String.t(), params(), list(query_option())) ::
:ok | {:error, Exception.t()}
def execute(client, statement, params \\ [], opts \\ []) do
opts = Keyword.merge(opts, output_format: :none, script: true, raw: true)
Expand All @@ -521,7 +528,7 @@ defmodule EdgeDB do

See `t:EdgeDB.query_option/0` for supported options.
"""
@spec execute!(client(), String.t(), list(), list(query_option())) :: :ok
@spec execute!(client(), String.t(), params(), list(query_option())) :: :ok
def execute!(client, statement, params \\ [], opts \\ []) do
opts = Keyword.merge(opts, output_format: :none, script: true, raw: true)
query!(client, statement, params, opts)
Expand Down Expand Up @@ -789,7 +796,7 @@ defmodule EdgeDB do
end

true ->
EdgeDB.Result.extract(result)
EdgeDB.Result.extract(result, Keyword.get(opts, :__transform_result__))
end
end

Expand Down
44 changes: 33 additions & 11 deletions lib/edgedb/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,13 @@ defmodule EdgeDB.Connection do
)

with {:ok, state} <- wait_for_server_ready(state) do
{:ok, %EdgeDB.Query{query | codec_storage: state.codec_storage}, state}
query = %EdgeDB.Query{
query
| codec_storage: state.codec_storage,
result_cardinality: message.result_cardinality
}

{:ok, query, state}
end
end

Expand Down Expand Up @@ -983,17 +989,19 @@ defmodule EdgeDB.Connection do
%Server.V0.PrepareComplete{
input_typedesc_id: in_id,
output_typedesc_id: out_id,
headers: %{capabilities: capabilities}
headers: %{capabilities: capabilities},
cardinality: result_cardinality
},
state
) do
with {:ok, state} <- wait_for_server_ready(state) do
maybe_legacy_describe_codecs(
%EdgeDB.Query{query | capabilities: capabilities},
in_id,
out_id,
state
)
query = %EdgeDB.Query{
query
| capabilities: capabilities,
result_cardinality: result_cardinality
}

maybe_legacy_describe_codecs(query, in_id, out_id, state)
end
end

Expand Down Expand Up @@ -1062,7 +1070,13 @@ defmodule EdgeDB.Connection do
)

with {:ok, state} <- wait_for_server_ready(state) do
{:ok, %EdgeDB.Query{query | codec_storage: state.codec_storage}, state}
query = %EdgeDB.Query{
query
| codec_storage: state.codec_storage,
result_cardinality: message.result_cardinality
}

{:ok, query, state}
end
end

Expand Down Expand Up @@ -1158,7 +1172,11 @@ defmodule EdgeDB.Connection do
query =
save_query_with_codecs_in_cache(
state.queries_cache,
%EdgeDB.Query{query | capabilities: capabilities},
%EdgeDB.Query{
query
| capabilities: capabilities,
result_cardinality: message.result_cardinality
},
message.input_typedesc_id,
message.output_typedesc_id
)
Expand Down Expand Up @@ -1311,7 +1329,11 @@ defmodule EdgeDB.Connection do
query =
save_query_with_codecs_in_cache(
state.queries_cache,
query,
%EdgeDB.Query{
query
| capabilities: message.capabilities,
result_cardinality: message.result_cardinality
},
message.input_typedesc_id,
message.output_typedesc_id
)
Expand Down
Loading