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

Initialize AshGraphQL and port tenantInfo query #430

Merged
Merged
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
62 changes: 57 additions & 5 deletions backend/lib/edgehog/tenants/tenant/tenant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,34 @@
defmodule Edgehog.Tenants.Tenant do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
extensions: [
AshGraphql.Resource,
AshJsonApi.Resource
]

alias Edgehog.Tenants.AstarteConfig
alias Edgehog.Tenants.Tenant
alias Edgehog.Validations

require Ash.Query

@type record :: Ash.Resource.record()

graphql do
type :tenant_info

# We don't care about filtering here
derive_filter? false
# :tenant_id, the primary key, already gets exposed as ID, so we hide it here
# to avoid showing it twice. We also hide the public key to be consistent with
# the old API
hide_fields [:tenant_id, :public_key]

queries do
read_one :tenant_info, :current_tenant, allow_nil?: false
end
end

json_api do
type "tenant"

Expand All @@ -54,6 +74,22 @@ defmodule Edgehog.Tenants.Tenant do

read :by_slug, get_by: :slug

read :current_tenant do
description "Retrieves the current tenant."
get? true

prepare fn query, _context ->
if query.tenant do
Ash.Query.filter(query, tenant_id: query.tenant.tenant_id)
else
Ash.Query.add_error(
query,
Ash.Error.Invalid.TenantRequired.exception(resource: query.resource)
)
end
end
end

create :provision do
argument :astarte_config, AstarteConfig, allow_nil?: false

Expand All @@ -74,10 +110,26 @@ defmodule Edgehog.Tenants.Tenant do
attributes do
integer_primary_key :tenant_id

attribute :name, :string, allow_nil?: false
attribute :slug, :string, allow_nil?: false
attribute :default_locale, :string, default: "en-US"
attribute :public_key, :string, allow_nil?: false
attribute :name, :string do
description "The tenant name."
allow_nil? false
end

attribute :slug, :string do
description "The tenant slug."
allow_nil? false
end

attribute :default_locale, :string do
description "The default locale supported by the tenant."
allow_nil? false
default "en-US"
end

attribute :public_key, :string do
description "The tenant public key."
allow_nil? false
end

create_timestamp :inserted_at
update_timestamp :updated_at
Expand Down
6 changes: 5 additions & 1 deletion backend/lib/edgehog/tenants/tenants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
#

defmodule Edgehog.Tenants do
use Ash.Api, extensions: [AshJsonApi.Api]
use Ash.Api,
extensions: [AshGraphql.Api, AshJsonApi.Api]

graphql do
end

json_api do
prefix "/admin-api/v1"
Expand Down
4 changes: 2 additions & 2 deletions backend/lib/edgehog_web/auth/verify_header.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2022 SECO Mind Srl
# Copyright 2022-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,7 +41,7 @@ defmodule EdgehogWeb.Auth.VerifyHeader do
end

defp get_public_key(conn) do
tenant = conn.assigns[:current_tenant]
tenant = Ash.PlugHelpers.get_tenant(conn)

case JWK.from_pem(tenant.public_key) do
%JWK{} = public_key ->
Expand Down
12 changes: 5 additions & 7 deletions backend/lib/edgehog_web/context.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2023 SECO Mind Srl
# Copyright 2021-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -25,18 +25,16 @@ defmodule EdgehogWeb.Context do

def call(conn, _opts) do
context = build_context(conn)
Absinthe.Plug.put_options(conn, context: context)
Ash.PlugHelpers.set_context(conn, context)
end

def build_context(conn) do
current_tenant = conn.assigns[:current_tenant]
tenant_locale = current_tenant.default_locale
tenant = Ash.PlugHelpers.get_tenant(conn)
preferred_locales = get_preferred_locales(conn)

%{
current_tenant: current_tenant,
preferred_locales: preferred_locales,
tenant_locale: tenant_locale
tenant_locale: tenant.default_locale,
preferred_locales: preferred_locales
}
end

Expand Down
5 changes: 2 additions & 3 deletions backend/lib/edgehog_web/populate_tenant.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2021-2023 SECO Mind Srl
# Copyright 2021-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,8 +32,7 @@ defmodule EdgehogWeb.PopulateTenant do

case Tenants.Tenant.fetch_by_slug(tenant_slug) do
{:ok, tenant} ->
_ = Edgehog.Repo.put_tenant_id(tenant.tenant_id)
Plug.Conn.assign(conn, :current_tenant, tenant)
Ash.PlugHelpers.set_tenant(conn, tenant)

{:error, %Ash.Error.Query.NotFound{}} ->
conn
Expand Down
25 changes: 0 additions & 25 deletions backend/lib/edgehog_web/resolvers/tenants.ex

This file was deleted.

1 change: 1 addition & 0 deletions backend/lib/edgehog_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule EdgehogWeb.Router do
plug EdgehogWeb.PopulateTenant
plug EdgehogWeb.Auth
plug EdgehogWeb.Context
plug AshGraphql.Plug
end

pipeline :triggers do
Expand Down
7 changes: 5 additions & 2 deletions backend/lib/edgehog_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ defmodule EdgehogWeb.Schema do
import_types EdgehogWeb.Schema.LabelingTypes
import_types EdgehogWeb.Schema.LocalizationTypes
import_types EdgehogWeb.Schema.OSManagementTypes
import_types EdgehogWeb.Schema.TenantsTypes
import_types EdgehogWeb.Schema.UpdateCampaignsTypes
import_types EdgehogWeb.Schema.VariantTypes
import_types Absinthe.Plug.Types
import_types Absinthe.Type.Custom

@apis [Edgehog.Tenants]

# TODO: remove define_relay_types?: false once we convert everything to Ash
use AshGraphql, apis: @apis, define_relay_types?: false

alias EdgehogWeb.Middleware
alias EdgehogWeb.Resolvers

Expand Down Expand Up @@ -118,7 +122,6 @@ defmodule EdgehogWeb.Schema do
import_fields :devices_queries
import_fields :groups_queries
import_fields :labeling_queries
import_fields :tenants_queries
import_fields :update_campaigns_queries
end

Expand Down
47 changes: 0 additions & 47 deletions backend/lib/edgehog_web/schema/tenants_types.ex

This file was deleted.

8 changes: 4 additions & 4 deletions backend/test/edgehog_web/auth_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2022-2023 SECO Mind Srl
# Copyright 2022-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,11 @@
#

defmodule EdgehogWeb.AuthTest do
use EdgehogWeb.ConnCase, async: true
# This can't be async: true since it modifies the Application env
rbino marked this conversation as resolved.
Show resolved Hide resolved
use EdgehogWeb.ConnCase, async: false
alias Edgehog.Config

# Use an unauthenticated conn so we can control auth manually
@moduletag :unauthenticated
@moduletag :ported_to_ash

@query """
{
Expand Down
8 changes: 5 additions & 3 deletions backend/test/edgehog_web/context_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# This file is part of Edgehog.
#
# Copyright 2022-2023 SECO Mind Srl
# Copyright 2022-2024 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -21,14 +21,16 @@
defmodule EdgehogWeb.ContextTest do
use EdgehogWeb.ConnCase, async: true

@moduletag :ported_to_ash

alias EdgehogWeb.Context

test "build_context/1 fetches preferred locales from accept-language header", %{
conn: conn,
tenant: tenant
} do
default_locale = tenant.default_locale
conn = Plug.Conn.assign(conn, :current_tenant, tenant)
conn = Ash.PlugHelpers.set_tenant(conn, tenant)

language_headers = [
{"accept-language", "it-IT,it;q=0.8,en-UK;q=0.6,en;q=0.4"},
Expand All @@ -50,7 +52,7 @@ defmodule EdgehogWeb.ContextTest do
tenant: tenant
} do
default_locale = tenant.default_locale
conn = Plug.Conn.assign(conn, :current_tenant, tenant)
conn = Ash.PlugHelpers.set_tenant(conn, tenant)

%{preferred_locales: [], tenant_locale: tenant_locale} = Context.build_context(conn)

Expand Down
Loading
Loading