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

Ensure _get_entities_with_data_loader is not using the default db session #5314

Open
wants to merge 1 commit into
base: release-1.1
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
3 changes: 2 additions & 1 deletion backend/infrahub/graphql/resolvers/single_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@ async def _get_entities_with_data_loader(
node = await loader.load(key=peer_id)
if not node:
return None
return await node.to_graphql(db=db, fields=node_fields, related_node_ids=related_node_ids)
async with db.start_session() as dbs:
return await node.to_graphql(db=dbs, fields=node_fields, related_node_ids=related_node_ids)
111 changes: 110 additions & 1 deletion backend/tests/unit/graphql/test_graphql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from infrahub.core.constants import BranchSupportType, InfrahubKind
from infrahub.core.manager import NodeManager
from infrahub.core.node import Node
from infrahub.core.schema import NodeSchema
from infrahub.core.schema import NodeSchema, SchemaRoot
from infrahub.core.schema.schema_branch import SchemaBranch
from infrahub.core.timestamp import Timestamp
from infrahub.database import InfrahubDatabase
Expand Down Expand Up @@ -641,6 +641,115 @@ async def test_double_nested_query(db: InfrahubDatabase, default_branch: Branch,
assert gql_params.context.related_node_ids == {p1.id, p2.id, c1.id, c2.id, c3.id}


async def test_nested_query_single_relationship(
db: InfrahubDatabase, default_branch: Branch, node_group_schema, data_schema
):
raw_schema = {
"version": "1.0",
"generics": [
{
"name": "Generic",
"namespace": "Location",
"hierarchical": True,
"attributes": [{"name": "name", "optional": False, "kind": "Text"}],
"relationships": [{"name": "devices", "peer": "InfraDevice", "cardinality": "many", "optional": True}],
}
],
"nodes": [
{
"name": "Device",
"namespace": "Infra",
"attributes": [{"name": "name", "kind": "Text", "optional": False}],
"relationships": [
{"name": "location", "peer": "LocationGeneric", "optional": False, "cardinality": "one"}
],
},
{
"name": "Site",
"namespace": "Location",
"inherit_from": ["LocationGeneric"],
"attributes": [{"name": "description", "optional": False, "kind": "Text"}],
},
],
}
schema = SchemaRoot(**raw_schema)
schema_branch = registry.schema.register_schema(schema=schema, branch=default_branch.name)

site_schema = schema_branch.get_node(name="LocationSite")
device_schema = schema_branch.get_node(name="InfraDevice")

site1 = await Node.init(db=db, schema=site_schema, branch=default_branch)
await site1.new(db=db, name="site1", description="test")
await site1.save(db=db)

device1 = await Node.init(db=db, schema=device_schema, branch=default_branch)
await device1.new(db=db, name="device1", location=site1)
await device1.save(db=db)

device2 = await Node.init(db=db, schema=device_schema, branch=default_branch)
await device2.new(db=db, name="device2", location=site1)
await device2.save(db=db)

query = """
fragment LocationData on LocationSite {
name {
value
}
devices {
edges {
node {
name {
value
}
}
}
}
}

query {
InfraDevice {
edges {
node {
name {
value
}
location {
node {
... LocationData
}
}
}
}
}
}
"""
gql_params = prepare_graphql_params(
db=db, include_mutation=False, include_subscription=False, branch=default_branch
)
result = await graphql(
schema=gql_params.schema,
source=query,
context_value=gql_params.context,
root_value=None,
variable_values={},
)

assert result.errors is None

result_per_name = {
result["node"]["name"]["value"]: result["node"] for result in result.data["InfraDevice"]["edges"]
}
assert sorted(result_per_name.keys()) == ["device1", "device2"]
Copy link
Contributor

Choose a reason for hiding this comment

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

might be nice to check all the data in this response while we have it
verified this passes locally

Suggested change
assert sorted(result_per_name.keys()) == ["device1", "device2"]
assert sorted(result_per_name.keys()) == ["device1", "device2"]
expected_location_data = {
"node": {
"name": {"value": "site1"},
"devices": {"edges": [{"node": {"name": {"value": "device1"}}}, {"node": {"name": {"value": "device2"}}}]},
}
}
assert result.data["InfraDevice"]["edges"][0]["node"]["location"] == expected_location_data
assert result.data["InfraDevice"]["edges"][1]["node"]["location"] == expected_location_data

expected_location_data = {
"node": {
"name": {"value": "site1"},
"devices": {"edges": [{"node": {"name": {"value": "device1"}}}, {"node": {"name": {"value": "device2"}}}]},
}
}
assert result.data["InfraDevice"]["edges"][0]["node"]["location"] == expected_location_data
assert result.data["InfraDevice"]["edges"][1]["node"]["location"] == expected_location_data


async def test_display_label_nested_query(
db: InfrahubDatabase, default_branch: Branch, car_person_schema: SchemaBranch
):
Expand Down
Loading