gpt4 book ai didi

java - GraphQL错误: Expected a user-defined GraphQL scalar type with name 'FileUpload' but found none

转载 作者:行者123 更新时间:2023-12-01 21:10:21 24 4
gpt4 key购买 nike

这几天我一直在尝试用 Java-GraphQL 实现上传文件。我找到了这个主题:How to upload files with graphql-java?我实现了第二个解决方案。

public class FileUpload {
private String contentType;
private byte[] content;

public FileUpload(String contentType, byte[] content) {
this.contentType = contentType;
this.content = content;
}

public String getContentType() {
return contentType;
}

public byte[] getContent() {
return content;
}
}
public class MyScalars {
public static final GraphQLScalarType FileUpload = new GraphQLScalarType(
"FileUpload",
"A file part in a multipart request",
new Coercing<FileUpload, Void>() {

@Override
public Void serialize(Object dataFetcherResult) {
throw new CoercingSerializeException("Upload is an input-only type");
}

@Override
public FileUpload parseValue(Object input) {
if (input instanceof Part) {
Part part = (Part) input;
try {
String contentType = part.getContentType();
byte[] content = new byte[part.getInputStream().available()];
part.delete();
return new FileUpload(contentType, content);

} catch (IOException e) {
throw new CoercingParseValueException("Couldn't read content of the uploaded file");
}
} else if (null == input) {
return null;
} else {
throw new CoercingParseValueException(
"Expected type " + Part.class.getName() + " but was " + input.getClass().getName());
}
}

@Override
public FileUpload parseLiteral(Object input) {
throw new CoercingParseLiteralException(
"Must use variables to specify Upload values");
}
});
}
public class FileUploadResolver implements GraphQLMutationResolver {

public Boolean uploadFile(FileUpload fileUpload) {

String fileContentType = fileUpload.getContentType();
byte[] fileContent = fileUpload.getContent();

// Do something in order to persist the file :)


return true;
}
}
scalar FileUpload

type Mutation {
uploadFile(fileUpload: FileUpload): Boolean
}

我在编译过程中遇到此错误:

原因:com.coxautodev.graphql.tools.SchemaClassScannerError:需要名称为“FileUpload”的用户定义的 GraphQL 标量类型,但没有找到!

最佳答案

您是否通过 RuntimeWiring 注册了它?

看这里:Custom Scalar in Graphql-java

关于java - GraphQL错误: Expected a user-defined GraphQL scalar type with name 'FileUpload' but found none,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58906119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com