@DgsData(parentType = DgsConstants.MUTATION.TYPE_NAME, field = "uploadScriptWithMultipartPOST")
public boolean uploadScript(DataFetchingEnvironment dfe) throws IOException {
// NOTE: Cannot use @InputArgument or Object Mapper to convert to class, because MultipartFile cannot be
// deserialized
MultipartFile file = dfe.getArgument("input");
String content = new String(file.getBytes());
return ! content.isEmpty();
}
注意你不能使用 Jackson Object Mapper 去反序列化包含了 MultipartFile 的类型,你需要在你的输入中显式的获得文件参数。
在你的客户端,你可以使用 apollo-upload-client 去发送你的作为一个 multipart POST 包含文件数据的 Mutation query。下面是怎样配置你的 link:
import { createUploadLink } from 'apollo-upload-client'
const uploadLink = createUploadLink({ uri: uri })
const authedClient = authLink && new ApolloClient({
link: uploadLink)),
cache: new InMemoryCache()
})