Skip to content

Commit f8570a8

Browse files
committed
feat: add tag subscription
- add GraphQL subscription for tag creation events - add test coverage for tag subscriptions to ensure functionality works as expected Signed-off-by: Osman Hadzic <osman.hadzic@secomind.com>
1 parent bec0758 commit f8570a8

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

backend/lib/edgehog/labeling/tag.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ defmodule Edgehog.Labeling.Tag do
3737
graphql do
3838
type :tag
3939

40+
subscriptions do
41+
pubsub EdgehogWeb.Endpoint
42+
43+
subscribe :tag do
44+
action_types [:create]
45+
end
46+
end
47+
4048
paginate_relationship_with device_tags: :relay
4149
end
4250

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# This file is part of Edgehog.
3+
#
4+
# Copyright 2026 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule EdgehogWeb.Schema.Subscriptions.Labeling.TagsSubscriptionsTest do
22+
@moduledoc false
23+
use EdgehogWeb.SubsCase
24+
25+
import Edgehog.LabelingFixtures
26+
27+
describe "Tag subscription" do
28+
test "receive data on tag creation", %{socket: socket, tenant: tenant} do
29+
subscribe(socket)
30+
31+
tag = tag_fixture(tenant: tenant)
32+
33+
assert_push "subscription:data", push
34+
35+
assert_created "tag", tag_data, push
36+
37+
assert tag_data["id"] == AshGraphql.Resource.encode_relay_id(tag)
38+
assert tag_data["name"] == tag.name
39+
end
40+
41+
defp subscribe(socket, opts \\ []) do
42+
default_sub_gql = """
43+
subscription Tag {
44+
tag {
45+
created {
46+
id
47+
name
48+
}
49+
}
50+
}
51+
"""
52+
53+
sub_gql = Keyword.get(opts, :query, default_sub_gql)
54+
55+
ref = push_doc(socket, sub_gql)
56+
assert_reply ref, :ok, %{subscriptionId: subscription_id}
57+
58+
subscription_id
59+
end
60+
end
61+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#
2+
# This file is part of Edgehog.
3+
#
4+
# Copyright 2026 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
20+
21+
defmodule Edgehog.LabelingFixtures do
22+
@moduledoc """
23+
This module defines test helpers for creating
24+
entities via the `Edgehog.Labeling` context.
25+
"""
26+
27+
@doc """
28+
Generate a unique tag name.
29+
"""
30+
def unique_tag_name, do: "tag_#{System.unique_integer([:positive])}"
31+
32+
@doc """
33+
Generate a tag.
34+
35+
## Options
36+
37+
* `:tenant` - Required. The tenant for the tag.
38+
"""
39+
def tag_fixture(opts \\ []) do
40+
{tenant, opts} = Keyword.pop!(opts, :tenant)
41+
42+
params =
43+
Enum.into(opts, %{
44+
name: unique_tag_name()
45+
})
46+
47+
Edgehog.Labeling.Tag
48+
|> Ash.Changeset.for_create(:create, params, tenant: tenant)
49+
|> Ash.create!()
50+
end
51+
end

0 commit comments

Comments
 (0)