【问题标题】:GraphQL Error: Expected a user-defined GraphQL scalar type with name 'FileUpload' but found noneGraphQL 错误:需要一个名为“FileUpload”的用户定义的 GraphQL 标量类型,但没有找到
【发布时间】:2020-03-13 07:46:55
【问题描述】:

这几天我一直在尝试用 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 标量类型,但没有找到!

【问题讨论】:

  • @Andreas Larson

标签: java graphql


【解决方案1】:

您是否通过 RuntimeWiring 注册了它?

看这里:Custom Scalar in Graphql-java

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2018-05-29
    • 2020-06-01
    • 2021-09-21
    • 2022-07-30
    • 1970-01-01
    • 2021-09-14
    • 2019-07-28
    • 2019-10-12
    相关资源
    最近更新 更多