@@ -22,12 +22,12 @@ struct HTTPTests {
2222 uri: " /graphql " ,
2323 method: . post,
2424 headers: jsonGraphQLHeaders,
25- body: . init( data: JSONEncoder ( ) . encode ( GraphQLRequest ( query: " { hello } " ) ) )
25+ body: . init( data: defaultJSONEncoder . encode ( GraphQLRequest ( query: " { hello } " ) ) )
2626 ) { response in
2727 #expect( response. status == . ok)
2828 #expect( response. headers [ . contentType] == " application/graphql-response+json; charset=utf-8 " )
2929
30- let result = try JSONDecoder ( ) . decode ( GraphQLResult . self, from: response. body)
30+ let result = try defaultJSONDecoder . decode ( GraphQLResult . self, from: response. body)
3131 #expect( result. data ? [ " hello " ] == " World " )
3232 #expect( result. errors. isEmpty)
3333 }
@@ -66,15 +66,15 @@ struct HTTPTests {
6666 uri: " /graphql " ,
6767 method: . post,
6868 headers: jsonGraphQLHeaders,
69- body: . init( data: JSONEncoder ( ) . encode ( GraphQLRequest (
69+ body: . init( data: defaultJSONEncoder . encode ( GraphQLRequest (
7070 query: " query Greet($name: String) { greet(name: $name) } " ,
7171 variables: [ " name " : " Alice " ]
7272 ) ) )
7373 ) { response in
7474 #expect( response. status == . ok)
7575 #expect( response. headers [ . contentType] == " application/graphql-response+json; charset=utf-8 " )
7676
77- let result = try JSONDecoder ( ) . decode ( GraphQLResult . self, from: response. body)
77+ let result = try defaultJSONDecoder . decode ( GraphQLResult . self, from: response. body)
7878 #expect( result. data ? [ " greet " ] == " Hello, Alice " )
7979 #expect( result. errors. isEmpty)
8080 }
@@ -114,12 +114,12 @@ struct HTTPTests {
114114 uri: " /graphql " ,
115115 method: . post,
116116 headers: jsonGraphQLHeaders,
117- body: . init( data: JSONEncoder ( ) . encode ( GraphQLRequest ( query: " { contextMessage } " ) ) )
117+ body: . init( data: defaultJSONEncoder . encode ( GraphQLRequest ( query: " { contextMessage } " ) ) )
118118 ) { response in
119119 #expect( response. status == . ok)
120120 #expect( response. headers [ . contentType] == " application/graphql-response+json; charset=utf-8 " )
121121
122- let result = try JSONDecoder ( ) . decode ( GraphQLResult . self, from: response. body)
122+ let result = try defaultJSONDecoder . decode ( GraphQLResult . self, from: response. body)
123123 #expect( result. data ? [ " contextMessage " ] == " Hello from context! " )
124124 #expect( result. errors. isEmpty)
125125 }
@@ -141,12 +141,12 @@ struct HTTPTests {
141141 . accept: MediaType . applicationJson. description,
142142 . contentType: MediaType . applicationJsonGraphQL. description,
143143 ] ,
144- body: . init( data: JSONEncoder ( ) . encode ( GraphQLRequest ( query: " { hello } " ) ) )
144+ body: . init( data: defaultJSONEncoder . encode ( GraphQLRequest ( query: " { hello } " ) ) )
145145 ) { response in
146146 #expect( response. status == . ok)
147147 #expect( response. headers [ . contentType] == " application/json; charset=utf-8 " )
148148
149- let result = try JSONDecoder ( ) . decode ( GraphQLResult . self, from: response. body)
149+ let result = try defaultJSONDecoder . decode ( GraphQLResult . self, from: response. body)
150150 #expect( result. data ? [ " hello " ] == " World " )
151151 #expect( result. errors. isEmpty)
152152 }
@@ -168,12 +168,12 @@ struct HTTPTests {
168168 . accept: MediaType . applicationJsonGraphQL. description,
169169 . contentType: MediaType . applicationJson. description,
170170 ] ,
171- body: . init( data: JSONEncoder ( ) . encode ( GraphQLRequest ( query: " { hello } " ) ) )
171+ body: . init( data: defaultJSONEncoder . encode ( GraphQLRequest ( query: " { hello } " ) ) )
172172 ) { response in
173173 #expect( response. status == . ok)
174174 #expect( response. headers [ . contentType] == " application/graphql-response+json; charset=utf-8 " )
175175
176- let result = try JSONDecoder ( ) . decode ( GraphQLResult . self, from: response. body)
176+ let result = try defaultJSONDecoder . decode ( GraphQLResult . self, from: response. body)
177177 #expect( result. data ? [ " hello " ] == " World " )
178178 #expect( result. errors. isEmpty)
179179 }
@@ -194,7 +194,7 @@ struct HTTPTests {
194194 headers: [
195195 . contentType: MediaType . applicationJsonGraphQL. description,
196196 ] ,
197- body: . init( data: JSONEncoder ( ) . encode ( GraphQLRequest ( query: " { hello } " ) ) )
197+ body: . init( data: defaultJSONEncoder . encode ( GraphQLRequest ( query: " { hello } " ) ) )
198198 ) { response in
199199 #expect( response. status == . notAcceptable)
200200 }
@@ -215,12 +215,12 @@ struct HTTPTests {
215215 headers: [
216216 . contentType: MediaType . applicationJsonGraphQL. description,
217217 ] ,
218- body: . init( data: JSONEncoder ( ) . encode ( GraphQLRequest ( query: " { hello } " ) ) )
218+ body: . init( data: defaultJSONEncoder . encode ( GraphQLRequest ( query: " { hello } " ) ) )
219219 ) { response in
220220 #expect( response. status == . ok)
221221 #expect( response. headers [ . contentType] == " application/graphql-response+json; charset=utf-8 " )
222222
223- let result = try JSONDecoder ( ) . decode ( GraphQLResult . self, from: response. body)
223+ let result = try defaultJSONDecoder . decode ( GraphQLResult . self, from: response. body)
224224 #expect( result. data ? [ " hello " ] == " World " )
225225 #expect( result. errors. isEmpty)
226226 }
@@ -242,7 +242,7 @@ struct HTTPTests {
242242 ) { response in
243243 #expect( response. status == . ok)
244244
245- let result = try JSONDecoder ( ) . decode ( GraphQLResult . self, from: response. body)
245+ let result = try defaultJSONDecoder . decode ( GraphQLResult . self, from: response. body)
246246 #expect( result. data ? [ " hello " ] == " World " )
247247 #expect( result. errors. isEmpty)
248248 }
@@ -311,4 +311,35 @@ struct HTTPTests {
311311 }
312312 }
313313 }
314+
315+ @Test func customEncoder( ) async throws {
316+ let graphQLJSONEncoder = GraphQLJSONEncoder ( )
317+ graphQLJSONEncoder. dateEncodingStrategy = . secondsSince1970
318+ let router = Router ( )
319+ router. graphql (
320+ schema: helloWorldSchema,
321+ config: . init(
322+ coders: . init( graphQLJSONEncoder: graphQLJSONEncoder)
323+ )
324+ ) { _, _ in
325+ EmptyContext ( )
326+ }
327+ let app = Application ( router: router)
328+
329+ try await app. test ( . router) { client in
330+ try await client. execute (
331+ uri: " /graphql " ,
332+ method: . post,
333+ headers: jsonGraphQLHeaders,
334+ body: . init( data: defaultJSONEncoder. encode ( GraphQLRequest ( query: " { hello } " ) ) )
335+ ) { response in
336+ #expect( response. status == . ok)
337+ #expect( response. headers [ . contentType] == " application/graphql-response+json; charset=utf-8 " )
338+
339+ let result = try defaultJSONDecoder. decode ( GraphQLResult . self, from: response. body)
340+ #expect( result. data ? [ " hello " ] == " World " )
341+ #expect( result. errors. isEmpty)
342+ }
343+ }
344+ }
314345}
0 commit comments