|
1 | 1 | from textwrap import dedent |
| 2 | +from typing import Annotated |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 |
|
@@ -998,3 +999,166 @@ def test_comprehensive(self, input: ComprehensiveInput) -> str: |
998 | 999 | """) |
999 | 1000 | assert not result5.errors # No error - both fields are optional in schema |
1000 | 1001 | assert result5.data == {"testComprehensive": "strict=absent, flexible=world"} |
| 1002 | + |
| 1003 | + |
| 1004 | +def test_maybe_with_explicit_field_description(): |
| 1005 | + """Handle case where strawberry.field annotation is used on a field with Maybe[T] type.""" |
| 1006 | + |
| 1007 | + @strawberry.input |
| 1008 | + class InputData: |
| 1009 | + name: strawberry.Maybe[str | None] = strawberry.field( |
| 1010 | + description="This strawberry.field annotation was breaking in default injection" |
| 1011 | + ) |
| 1012 | + |
| 1013 | + @strawberry.type |
| 1014 | + class Query: |
| 1015 | + @strawberry.field |
| 1016 | + def test(self, data: InputData) -> str: |
| 1017 | + if data.name is None: |
| 1018 | + return "I am a test, and I received: None" |
| 1019 | + return "I am a test, and I received: " + str(data.name.value) |
| 1020 | + |
| 1021 | + schema = strawberry.Schema(Query) |
| 1022 | + |
| 1023 | + assert str(schema) == dedent( |
| 1024 | + '''\ |
| 1025 | + input InputData { |
| 1026 | + """This strawberry.field annotation was breaking in default injection""" |
| 1027 | + name: String |
| 1028 | + } |
| 1029 | +
|
| 1030 | + type Query { |
| 1031 | + test(data: InputData!): String! |
| 1032 | + }''' |
| 1033 | + ) |
| 1034 | + |
| 1035 | + query1 = """ |
| 1036 | + query { |
| 1037 | + test(data: { name: null }) |
| 1038 | + } |
| 1039 | + """ |
| 1040 | + result1 = schema.execute_sync(query1) |
| 1041 | + assert not result1.errors |
| 1042 | + |
| 1043 | + query2 = """ |
| 1044 | + query { |
| 1045 | + test(data: { name: "hello" }) |
| 1046 | + } |
| 1047 | + """ |
| 1048 | + result2 = schema.execute_sync(query2) |
| 1049 | + assert not result2.errors |
| 1050 | + |
| 1051 | + query3 = """ |
| 1052 | + query { |
| 1053 | + test(data: {}) |
| 1054 | + } |
| 1055 | + """ |
| 1056 | + result3 = schema.execute_sync(query3) |
| 1057 | + assert not result3.errors |
| 1058 | + |
| 1059 | + |
| 1060 | +def test_maybe_wrapped_with_annotated_typing(): |
| 1061 | + """Handle case where Maybe is wrapped with Annotated typing.""" |
| 1062 | + |
| 1063 | + @strawberry.input |
| 1064 | + class InputData: |
| 1065 | + name: Annotated[strawberry.Maybe[str | None], "some meta"] |
| 1066 | + |
| 1067 | + @strawberry.type |
| 1068 | + class Query: |
| 1069 | + @strawberry.field |
| 1070 | + def test(self, data: InputData) -> str: |
| 1071 | + if data.name is None: |
| 1072 | + return "I am a test, and I received: None" |
| 1073 | + return "I am a test, and I received: " + str(data.name.value) |
| 1074 | + |
| 1075 | + schema = strawberry.Schema(Query) |
| 1076 | + |
| 1077 | + assert str(schema) == dedent( |
| 1078 | + """\ |
| 1079 | + input InputData { |
| 1080 | + name: String |
| 1081 | + } |
| 1082 | +
|
| 1083 | + type Query { |
| 1084 | + test(data: InputData!): String! |
| 1085 | + }""" |
| 1086 | + ) |
| 1087 | + query1 = """ |
| 1088 | + query { |
| 1089 | + test(data: { name: null }) |
| 1090 | + } |
| 1091 | + """ |
| 1092 | + result1 = schema.execute_sync(query1) |
| 1093 | + assert not result1.errors |
| 1094 | + |
| 1095 | + query2 = """ |
| 1096 | + query { |
| 1097 | + test(data: { name: "hello" }) |
| 1098 | + } |
| 1099 | + """ |
| 1100 | + result2 = schema.execute_sync(query2) |
| 1101 | + assert not result2.errors |
| 1102 | + |
| 1103 | + query3 = """ |
| 1104 | + query { |
| 1105 | + test(data: {}) |
| 1106 | + } |
| 1107 | + """ |
| 1108 | + result3 = schema.execute_sync(query3) |
| 1109 | + assert not result3.errors |
| 1110 | + |
| 1111 | + |
| 1112 | +def test_maybe_with_annotated_and_explicit_definition(): |
| 1113 | + """Handle case where Maybe is wrapped with Annotated typing.""" |
| 1114 | + |
| 1115 | + @strawberry.input |
| 1116 | + class InputData: |
| 1117 | + name: Annotated[strawberry.Maybe[str | None], "some meta"] = strawberry.field( |
| 1118 | + description="This strawberry.field annotation was breaking in default injection" |
| 1119 | + ) |
| 1120 | + |
| 1121 | + @strawberry.type |
| 1122 | + class Query: |
| 1123 | + @strawberry.field |
| 1124 | + def test(self, data: InputData) -> str: |
| 1125 | + if data.name is None: |
| 1126 | + return "I am a test, and I received: None" |
| 1127 | + return "I am a test, and I received: " + str(data.name.value) |
| 1128 | + |
| 1129 | + schema = strawberry.Schema(Query) |
| 1130 | + |
| 1131 | + assert str(schema) == dedent( |
| 1132 | + '''\ |
| 1133 | + input InputData { |
| 1134 | + """This strawberry.field annotation was breaking in default injection""" |
| 1135 | + name: String |
| 1136 | + } |
| 1137 | +
|
| 1138 | + type Query { |
| 1139 | + test(data: InputData!): String! |
| 1140 | + }''' |
| 1141 | + ) |
| 1142 | + query1 = """ |
| 1143 | + query { |
| 1144 | + test(data: { name: null }) |
| 1145 | + } |
| 1146 | + """ |
| 1147 | + result1 = schema.execute_sync(query1) |
| 1148 | + assert not result1.errors |
| 1149 | + |
| 1150 | + query2 = """ |
| 1151 | + query { |
| 1152 | + test(data: { name: "hello" }) |
| 1153 | + } |
| 1154 | + """ |
| 1155 | + result2 = schema.execute_sync(query2) |
| 1156 | + assert not result2.errors |
| 1157 | + |
| 1158 | + query3 = """ |
| 1159 | + query { |
| 1160 | + test(data: {}) |
| 1161 | + } |
| 1162 | + """ |
| 1163 | + result3 = schema.execute_sync(query3) |
| 1164 | + assert not result3.errors |
0 commit comments