19
19
import java .util .Collection ;
20
20
import java .util .List ;
21
21
22
+ import graphql .schema .DataFetcher ;
23
+ import graphql .schema .FieldCoordinates ;
24
+ import graphql .schema .GraphQLFieldDefinition ;
25
+ import graphql .schema .GraphQLSchema ;
22
26
import graphql .schema .PropertyDataFetcher ;
27
+ import graphql .schema .SchemaTransformer ;
28
+ import graphql .schema .idl .RuntimeWiring ;
29
+ import graphql .schema .idl .SchemaGenerator ;
30
+ import graphql .schema .idl .SchemaParser ;
31
+ import graphql .schema .idl .TypeDefinitionRegistry ;
32
+ import org .junit .jupiter .api .Nested ;
23
33
import org .junit .jupiter .api .Test ;
24
34
import reactor .core .publisher .Mono ;
25
35
36
+ import org .springframework .core .io .Resource ;
26
37
import org .springframework .graphql .BookSource ;
27
38
import org .springframework .graphql .ExecutionGraphQlResponse ;
28
39
import org .springframework .graphql .GraphQlSetup ;
29
40
import org .springframework .graphql .ResponseHelper ;
41
+ import org .springframework .graphql .execution .ConnectionTypeDefinitionConfigurer ;
42
+
43
+ import static org .assertj .core .api .Assertions .assertThat ;
30
44
31
45
/**
32
46
* Unit tests for {@link ConnectionFieldTypeVisitor}.
@@ -69,18 +83,6 @@ void paginationDataFetcher() {
69
83
);
70
84
}
71
85
72
- @ Test // gh-707
73
- void trivialDataFetcherIsSkipped () {
74
-
75
- Mono <ExecutionGraphQlResponse > response = GraphQlSetup .schemaResource (BookSource .paginationSchema )
76
- .dataFetcher ("Query" , "books" , new PropertyDataFetcher <>("books" ))
77
- .connectionSupport (new ListConnectionAdapter ())
78
- .toGraphQlService ()
79
- .execute (BookSource .booksConnectionQuery (null ));
80
-
81
- ResponseHelper .forResponse (response ).assertData ("{\" books\" :null}" );
82
- }
83
-
84
86
@ Test // gh-707
85
87
void nullValueTreatedAsEmptyConnection () {
86
88
@@ -103,6 +105,85 @@ void nullValueTreatedAsEmptyConnection() {
103
105
}
104
106
105
107
108
+ @ Nested
109
+ class DecorationTests {
110
+
111
+ @ Test // gh-707
112
+ void trivialDataFetcherIsNotDecorated () throws Exception {
113
+
114
+ FieldCoordinates coordinates = FieldCoordinates .coordinates ("Query" , "books" );
115
+ DataFetcher <?> dataFetcher = new PropertyDataFetcher <>("books" );
116
+
117
+ DataFetcher <?> actual =
118
+ applyConnectionFieldTypeVisitor (BookSource .paginationSchema , coordinates , dataFetcher );
119
+
120
+ assertThat (actual ).isSameAs (dataFetcher );
121
+ }
122
+
123
+ @ Test // gh-709
124
+ void connectionTypeWithoutEdgesIsNotDecorated () throws Exception {
125
+
126
+ String schemaContent = """
127
+ type Query {
128
+ puzzles: PuzzleConnection
129
+ }
130
+ type PuzzleConnection {
131
+ pageInfo: PuzzlePageInfo!
132
+ puzzles: [PuzzleEdge]
133
+ }
134
+ type PuzzlePageInfo {
135
+ total: Int!
136
+ }
137
+ type PuzzleEdge {
138
+ puzzle: Puzzle
139
+ cursor: String!
140
+ }
141
+ type Puzzle {
142
+ title: String!
143
+ }
144
+ """ ;
145
+
146
+ FieldCoordinates coordinates = FieldCoordinates .coordinates ("Query" , "puzzles" );
147
+ DataFetcher <?> dataFetcher = env -> null ;
148
+
149
+ DataFetcher <?> actual = applyConnectionFieldTypeVisitor (schemaContent , coordinates , dataFetcher );
150
+
151
+ assertThat (actual ).isSameAs (dataFetcher );
152
+ }
153
+
154
+ private static DataFetcher <?> applyConnectionFieldTypeVisitor (
155
+ Object schemaSource , FieldCoordinates coordinates , DataFetcher <?> fetcher ) throws Exception {
156
+
157
+ TypeDefinitionRegistry registry ;
158
+ if (schemaSource instanceof Resource resource ) {
159
+ registry = new SchemaParser ().parse (resource .getInputStream ());
160
+ }
161
+ else if (schemaSource instanceof String schemaContent ) {
162
+ registry = new SchemaParser ().parse (schemaContent );
163
+ }
164
+ else {
165
+ throw new IllegalArgumentException ();
166
+ }
167
+
168
+ new ConnectionTypeDefinitionConfigurer ().configure (registry );
169
+
170
+ GraphQLSchema schema = new SchemaGenerator ().makeExecutableSchema (
171
+ registry , RuntimeWiring .newRuntimeWiring ()
172
+ .type (coordinates .getTypeName (), b -> b .dataFetcher (coordinates .getFieldName (), fetcher ))
173
+ .build ());
174
+
175
+ ConnectionFieldTypeVisitor visitor =
176
+ ConnectionFieldTypeVisitor .create (List .of (new ListConnectionAdapter ()));
177
+
178
+ schema = new SchemaTransformer ().transform (schema , visitor );
179
+ GraphQLFieldDefinition field = schema .getFieldDefinition (coordinates );
180
+ return schema .getCodeRegistry ().getDataFetcher (coordinates , field );
181
+ }
182
+
183
+ }
184
+
185
+
186
+
106
187
private static class ListConnectionAdapter implements ConnectionAdapter {
107
188
108
189
private int initialOffset = 0 ;
0 commit comments