spring Sping Boot GraphQL多部分文件上传测试

g6baxovj  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(55)

如何使用Sping Boot GraphQLTester测试文件上传。当我使用curl时,请求看起来

curl localhost:8080/graphql \
  -F operations='{ "query": "mutation ($file: Upload!) { singleUpload(file: $file) }", "variables": { "file": null } }' \
  -F map='{ "0": ["variables.file"] }' \
  -F [email protected]

字符串
问题是我无法从客户端设置表单数据

@GraphQlTest(FileController.class)
@Import({GraphQLTestConfiguration.class})
class FileGraphQLControllerTest {

  @Autowired
  private GraphQlTester graphQlTester;


graphQlTester客户端中,我没有将文件设置为数据表单的方法

String mutation = """
                mutation ($file: Upload!) {
                    singleUpload(file: $file)
                }
                """;

 graphQlTester.document(mutation) // how to set file here
            .execute()
            .path("/singleUpload")
            .get();

sz81bmfz

sz81bmfz1#

Spring GraphQL还不支持文件上传,请查看这里的讨论,https://github.com/spring-projects/spring-graphql/pull/430,所以目前Spring GraphQL测试工具还没有准备好上传文件。
如果你已经实现了GraphQL多文件上传功能(例如通过https://github.com/nkonev/multipart-spring-graphql或使用其他GraphQL框架进行后端),在测试中,你可以使用原始的Spring RestTemplateWebClient(用于webflux堆栈)将文件发送到后端服务器。
一个使用TestRestTemplate的例子在这里:https://github.com/hantsy/spring-graphql-sample/blob/master/graphql-java-kickstart/src/test/java/com/example/demo/DemoApplicationTests.java#L77,阅读GraphQL file upload best practice以了解相关的模式定义。

相关问题