@@ -1400,6 +1400,14 @@ async def departments(self) -> List[Department]:
1400
1400
name
1401
1401
}
1402
1402
}
1403
+ },
1404
+ building {
1405
+ edges {
1406
+ node {
1407
+ id
1408
+ name
1409
+ }
1410
+ }
1403
1411
}
1404
1412
}
1405
1413
}
@@ -1452,6 +1460,16 @@ async def departments(self) -> List[Department]:
1452
1460
}
1453
1461
}
1454
1462
]
1463
+ },
1464
+ "building" : {
1465
+ "edges" : [
1466
+ {
1467
+ "node" : {
1468
+ "id" : 2 ,
1469
+ "name" : "Building 1"
1470
+ }
1471
+ }
1472
+ ]
1455
1473
}
1456
1474
}
1457
1475
},
@@ -1469,6 +1487,16 @@ async def departments(self) -> List[Department]:
1469
1487
}
1470
1488
}
1471
1489
]
1490
+ },
1491
+ "building" : {
1492
+ "edges" : [
1493
+ {
1494
+ "node" : {
1495
+ "id" : 2 ,
1496
+ "name" : "Building 1"
1497
+ }
1498
+ }
1499
+ ]
1472
1500
}
1473
1501
}
1474
1502
}
@@ -1494,6 +1522,16 @@ async def departments(self) -> List[Department]:
1494
1522
}
1495
1523
}
1496
1524
]
1525
+ },
1526
+ "building" : {
1527
+ "edges" : [
1528
+ {
1529
+ "node" : {
1530
+ "id" : 2 ,
1531
+ "name" : "Building 1"
1532
+ }
1533
+ }
1534
+ ]
1497
1535
}
1498
1536
}
1499
1537
}
@@ -1900,3 +1938,229 @@ async def employees(self) -> List[Employee]:
1900
1938
}
1901
1939
]
1902
1940
}
1941
+
1942
+
1943
+ @pytest .fixture
1944
+ def secondary_tables_with_normal_relationship (base ):
1945
+ EmployeeDepartmentJoinTable = Table (
1946
+ "employee_department_join_table" ,
1947
+ base .metadata ,
1948
+ Column ("employee_id" , ForeignKey ("employee.id" ), primary_key = True ),
1949
+ Column ("department_id" , ForeignKey (
1950
+ "department.id" ), primary_key = True ),
1951
+ )
1952
+
1953
+ class Employee (base ):
1954
+ __tablename__ = "employee"
1955
+ id = Column (Integer , autoincrement = True , primary_key = True )
1956
+ name = Column (String , nullable = False )
1957
+ role = Column (String , nullable = False )
1958
+ department = relationship (
1959
+ "Department" ,
1960
+ secondary = "employee_department_join_table" ,
1961
+ back_populates = "employees" ,
1962
+ )
1963
+ building_id = Column (Integer , ForeignKey ("building.id" ))
1964
+ building = relationship (
1965
+ "Building" ,
1966
+ back_populates = "employees" ,
1967
+ )
1968
+
1969
+ class Department (base ):
1970
+ __tablename__ = "department"
1971
+ id = Column (Integer , autoincrement = True , primary_key = True )
1972
+ name = Column (String , nullable = False )
1973
+ employees = relationship (
1974
+ "Employee" ,
1975
+ secondary = "employee_department_join_table" ,
1976
+ back_populates = "department" ,
1977
+ )
1978
+
1979
+ class Building (base ):
1980
+ __tablename__ = "building"
1981
+ id = Column (Integer , autoincrement = True , primary_key = True )
1982
+ name = Column (String , nullable = False )
1983
+ employees = relationship (
1984
+ "Employee" ,
1985
+ back_populates = "building" ,
1986
+ )
1987
+
1988
+ return Employee , Department , Building
1989
+
1990
+
1991
+ @pytest .mark .asyncio
1992
+ async def test_query_with_secondary_table_with_values_list_and_normal_relationship (
1993
+ secondary_tables_with_normal_relationship ,
1994
+ base ,
1995
+ async_engine ,
1996
+ async_sessionmaker
1997
+ ):
1998
+ async with async_engine .begin () as conn :
1999
+ await conn .run_sync (base .metadata .create_all )
2000
+
2001
+ mapper = StrawberrySQLAlchemyMapper ()
2002
+ EmployeeModel , DepartmentModel , BuildingModel = secondary_tables_with_normal_relationship
2003
+
2004
+ @mapper .type (DepartmentModel )
2005
+ class Department ():
2006
+ pass
2007
+
2008
+ @mapper .type (EmployeeModel )
2009
+ class Employee ():
2010
+ pass
2011
+
2012
+ @mapper .type (BuildingModel )
2013
+ class Building ():
2014
+ pass
2015
+
2016
+ @strawberry .type
2017
+ class Query :
2018
+ @strawberry .field
2019
+ async def departments (self ) -> List [Department ]:
2020
+ async with async_sessionmaker () as session :
2021
+ result = await session .execute (select (DepartmentModel ))
2022
+ return result .scalars ().all ()
2023
+
2024
+ mapper .finalize ()
2025
+ schema = strawberry .Schema (query = Query )
2026
+
2027
+ query = """\
2028
+ query {
2029
+ departments {
2030
+ id
2031
+ name
2032
+ employees {
2033
+ edges {
2034
+ node {
2035
+ id
2036
+ name
2037
+ role
2038
+ department {
2039
+ edges {
2040
+ node {
2041
+ id
2042
+ name
2043
+ }
2044
+ }
2045
+ },
2046
+ building {
2047
+ id
2048
+ name
2049
+ }
2050
+ }
2051
+ }
2052
+ }
2053
+ }
2054
+ }
2055
+ """
2056
+
2057
+ # Create test data
2058
+ async with async_sessionmaker (expire_on_commit = False ) as session :
2059
+ building = BuildingModel (id = 2 , name = "Building 1" )
2060
+ department1 = DepartmentModel (id = 10 , name = "Department Test 1" )
2061
+ department2 = DepartmentModel (id = 3 , name = "Department Test 2" )
2062
+ e1 = EmployeeModel (id = 1 , name = "John" , role = "Developer" )
2063
+ e2 = EmployeeModel (id = 5 , name = "Bill" , role = "Doctor" )
2064
+ e3 = EmployeeModel (id = 4 , name = "Maria" , role = "Teacher" )
2065
+ department1 .employees .append (e1 )
2066
+ department1 .employees .append (e2 )
2067
+ department2 .employees .append (e3 )
2068
+ building .employees .append (e1 )
2069
+ building .employees .append (e2 )
2070
+ building .employees .append (e3 )
2071
+ session .add_all ([department1 , department2 , e1 , e2 , e3 , building ])
2072
+ await session .commit ()
2073
+
2074
+ result = await schema .execute (query , context_value = {
2075
+ "sqlalchemy_loader" : StrawberrySQLAlchemyLoader (
2076
+ async_bind_factory = async_sessionmaker
2077
+ )
2078
+ })
2079
+ assert result .errors is None
2080
+ assert result .data == {
2081
+ "departments" : [
2082
+ {
2083
+ "id" : 10 ,
2084
+ "name" : "Department Test 1" ,
2085
+ "employees" : {
2086
+ "edges" : [
2087
+ {
2088
+ "node" : {
2089
+ "id" : 5 ,
2090
+ "name" : "Bill" ,
2091
+ "role" : "Doctor" ,
2092
+ "department" : {
2093
+ "edges" : [
2094
+ {
2095
+ "node" : {
2096
+ "id" : 10 ,
2097
+ "name" : "Department Test 1"
2098
+ }
2099
+ }
2100
+ ]
2101
+ },
2102
+ "building" : {
2103
+ "id" : 2 ,
2104
+ "name" : "Building 1"
2105
+ }
2106
+ }
2107
+ },
2108
+ {
2109
+ "node" : {
2110
+ "id" : 1 ,
2111
+ "name" : "John" ,
2112
+ "role" : "Developer" ,
2113
+ "department" : {
2114
+ "edges" : [
2115
+ {
2116
+ "node" : {
2117
+ "id" : 10 ,
2118
+ "name" : "Department Test 1"
2119
+ }
2120
+ }
2121
+ ]
2122
+ },
2123
+ "building" : {
2124
+ "id" : 2 ,
2125
+ "name" : "Building 1"
2126
+ }
2127
+ }
2128
+ }
2129
+ ]
2130
+ }
2131
+ },
2132
+ {
2133
+ "id" : 3 ,
2134
+ "name" : "Department Test 2" ,
2135
+ "employees" : {
2136
+ "edges" : [
2137
+ {
2138
+ "node" : {
2139
+ "id" : 4 ,
2140
+ "name" : "Maria" ,
2141
+ "role" : "Teacher" ,
2142
+ "department" : {
2143
+ "edges" : [
2144
+ {
2145
+ "node" : {
2146
+ "id" : 3 ,
2147
+ "name" : "Department Test 2"
2148
+ }
2149
+ }
2150
+ ]
2151
+ },
2152
+ "building" : {
2153
+ "id" : 2 ,
2154
+ "name" : "Building 1"
2155
+ }
2156
+ }
2157
+ }
2158
+ ]
2159
+ }
2160
+ }
2161
+ ]
2162
+ }
2163
+
2164
+
2165
+ # TODO
2166
+ # Make test with secondary table and normal relationship at same time
0 commit comments