Skip to content

Commit bec06db

Browse files
committed
Move test-mode-related parts from paths/schema.py to separate file
1 parent 651b59f commit bec06db

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

paths/schema.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,28 +122,21 @@ def node(self, info: SBInfo, id: str) -> SBNode:
122122
return SBNode(id=cast(sb.ID, node.id))
123123

124124

125-
SB_MUTATION_TYPES: list[type] = []
126-
if test_mode_enabled():
127-
SB_MUTATION_TYPES.append(TestModeMutations)
128-
129-
SBMutation: type | None = None
130-
if SB_MUTATION_TYPES:
131-
SBMutation = merge_types('Mutation', tuple(SB_MUTATION_TYPES))
132-
133-
134-
def generate_strawberry_schema() -> sb.Schema:
125+
def generate_strawberry_schema(query: type, mutation: type | None = None) -> sb.Schema:
135126
from kausal_common.strawberry.registry import strawberry_types
136127

137128
sb_schema = sb.Schema(
138-
query=SBQuery, mutation=SBMutation, types=strawberry_types, directives=[context_directive]
129+
# TODO: Add DjangoOptimizerExtension?
130+
# https://strawberry.rocks/docs/django/guide/optimizer
131+
query=query, mutation=mutation, types=strawberry_types, directives=[context_directive]
139132
)
140133
return sb_schema
141134

142135

143-
def generate_schema() -> tuple[sb.Schema, CombinedSchema]:
136+
def generate_schema(sb_query: type, sb_mutation: type | None = None) -> tuple[sb.Schema, CombinedSchema]:
144137
# We generate the Strawberry schema just to be able to utilize the
145138
# resolved GraphQL types directly in the Graphene schema.
146-
sb_schema = generate_strawberry_schema()
139+
sb_schema = generate_strawberry_schema(sb_query, sb_mutation)
147140

148141
schema = CombinedSchema(
149142
sb_schema=sb_schema,
@@ -155,4 +148,4 @@ def generate_schema() -> tuple[sb.Schema, CombinedSchema]:
155148
return sb_schema, schema
156149

157150

158-
sb_schema, schema = generate_schema()
151+
sb_schema, schema = generate_schema(SBQuery)

paths/schema_test_mode.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import annotations
2+
3+
from strawberry.tools import merge_types
4+
5+
from kausal_common.deployment import test_mode_enabled
6+
from kausal_common.testing.schema import TestModeMutations
7+
8+
from paths.schema import SBQuery, generate_schema
9+
10+
SB_MUTATION_TYPES: list[type] = []
11+
if test_mode_enabled():
12+
SB_MUTATION_TYPES.append(TestModeMutations)
13+
14+
SBMutation: type | None = None
15+
if SB_MUTATION_TYPES:
16+
SBMutation = merge_types('Mutation', tuple(SB_MUTATION_TYPES))
17+
18+
sb_schema, schema = generate_schema(SBQuery, SBMutation)

paths/settings.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from corsheaders.defaults import default_headers as default_cors_headers
1616

1717
from kausal_common import ENV_SCHEMA as COMMON_ENV_SCHEMA, register_settings as register_common_settings
18-
from kausal_common.deployment import set_secret_file_vars
18+
from kausal_common.deployment import set_secret_file_vars, test_mode_enabled
1919
from kausal_common.deployment.http import get_allowed_cors_headers
2020
from kausal_common.sentry.init import init_sentry
2121

@@ -290,8 +290,12 @@
290290
SESSION_COOKIE_SAMESITE = 'None'
291291
SESSION_COOKIE_SECURE = True
292292

293+
if test_mode_enabled():
294+
GRAPHENE_SCHEMA = f'{PROJECT_NAME}.schema_test_mode.schema'
295+
else:
296+
GRAPHENE_SCHEMA = f'{PROJECT_NAME}.schema.schema'
293297
GRAPHENE = {
294-
'SCHEMA': f'{PROJECT_NAME}.schema.schema',
298+
'SCHEMA': GRAPHENE_SCHEMA,
295299
}
296300
GRAPPLE = {
297301
'APPS': ['pages'],

0 commit comments

Comments
 (0)