spring-boot - 预期类型 'Upload' 为 GraphQLInputType 是仅允许错误用作输入类型的对象类型的类型,反之亦然

标签 spring-boot gradle graphql

这里我尝试在Graphql中接收上传文件。我的代码如下

Graphql 架构 example.graphqls

scalar Upload
type Mutation {
    uploadFile(input: CreateFileUploadInput): Boolean
}

input CreateFileUploadInput {
    files: Upload
    id: String
}

Graphql标量上传在GraphqlConfig.java中定义

@Configuration
public class GraphqlConfig {

  @Bean
  public SchemaParserOptions schemaParserOptions(
          GraphQlObjectMapperConfigurer customObjectMapperConfigurer) {
    return SchemaParserOptions.newOptions().objectMapperConfigurer(customObjectMapperConfigurer)
            .build();
  }

  @Bean
  GraphQLScalarType upload() {
    return graphql.servlet.ApolloScalars.Upload;
  }
}

Graphql Objectmapper 配置器GraphQlObjectMapperConfigurer.java

@Component
public class GraphQlObjectMapperConfigurer  implements ObjectMapperConfigurer {
  @Override
  public void configure(ObjectMapper mapper, ObjectMapperConfigurerContext context) {
    mapper.registerModule(new JavaTimeModule())
        .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
        .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
        .setTimeZone(TimeZone.getDefault());
  }
}

我的模型类CreateFileUploadInput.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateFileUploadInput {
  private Part files;
  private String id;
}

我在build.gradle中使用的graphql spring boot版本是5.8.1和gradlegradle-5.6.2-bin

当我运行 Spring Boot 应用程序时,我遇到了异常!

2020-01-17 15:20:24.618  WARN 37316 --- [           main] c.c.graphql.tools.SchemaClassScanner     : Cannot find definition for field 'files: Upload' on input type 'CreateFileUploadInput' -> javax.servlet.http.Part. Try adding it manually to the dictionary
2020-01-17 15:20:24.665  WARN 37316 --- [           main] c.c.graphql.tools.SchemaClassScanner     : Schema type was defined but can never be accessed, and can be safely deleted: Upload
2020-01-17 15:20:24.665  WARN 37316 --- [           main] c.c.graphql.tools.SchemaClassScanner     : Schema type was defined but can never be accessed, and can be safely deleted: PageInfo
2020-01-17 15:20:24.752 ERROR 37316 --- [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'graphQLServletRegistrationBean' defined in class path resource [com/oembedler/moon/graphql/boot/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLServletRegistrationBean' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLHttpServlet' defined in class path resource [com/oembedler/moon/graphql/boot/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLHttpServlet' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLServletConfiguration' defined in class path resource [com/oembedler/moon/graphql/boot/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLServletConfiguration' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'invocationInputFactory' defined in class path resource [com/oembedler/moon/graphql/boot/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'invocationInputFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'graphQLSchemaProvider' defined in class path resource [com/oembedler/moon/graphql/boot/GraphQLWebAutoConfiguration.class]: Unsatisfied dependency expressed through method 'graphQLSchemaProvider' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphQLSchema' defined in class path resource [com/oembedler/moon/graphql/boot/GraphQLJavaToolsAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.schema.GraphQLSchema]: Factory method 'graphQLSchema' threw exception; nested exception is com.coxautodev.graphql.tools.SchemaError: Expected type 'Upload' to be a GraphQLInputType, but it wasn't!  Was a type only permitted for object types incorrectly used as an input type, or vice-versa?
2020-01-17 15:20:24.789  INFO 37316 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-01-17 15:20:24.794  WARN 37316 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [HikariPool-1 housekeeper] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [graphql.schema.GraphQLSchema]: Factory method 'graphQLSchema' threw exception; nested exception is com.coxautodev.graphql.tools.SchemaError: Expected type 'Upload' to be a GraphQLInputType, but it wasn't!  Was a type only permitted for object types incorrectly used as an input type, or vice-versa?
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
    ... 128 common frames omitted
Caused by: com.coxautodev.graphql.tools.SchemaError: Expected type 'Upload' to be a GraphQLInputType, but it wasn't!  Was a type only permitted for object types incorrectly used as an input type, or vice-versa?
    at com.coxautodev.graphql.tools.SchemaParser.determineType(SchemaParser.kt:350) ~[graphql-java-tools-5.6.0.jar:na]

我花了很多时间调查这个问题,但还是无法解决。请帮忙!

最佳答案

当我尝试使用 type 而不是 input 作为输入时,遇到了此错误,这是不允许的。这与原来的问题略有不同,但您会看到相同的错误:

所以这不起作用:

scalar Upload
type Mutation {
    uploadFile(input: CreateFileUploadInput): Boolean
}

type CreateFileUploadInput {
    files: Upload
    id: String
}

这解决了它:

input CreateFileUploadInput {
    files: Upload
    id: String
}

(只需将其从type更改为input)

关于spring-boot - 预期类型 'Upload' 为 GraphQLInputType 是仅允许错误用作输入类型的对象类型的类型,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59785295/

相关文章:

java - 未使用 Spring Webflux 路由器功能

gradle - Groovy:多次声明字段属性

Gradle doLast,复制任务中的 doFirst 行为

javascript - 乐观地更新 UI 时缺少字段 '_id' 、 'owner' 、 'meta' 和 'comment'

java - Spring Security HttpSecurity 配置

spring - 如何在spring boot和嵌入式tomcat中设置mod_reqtimeout?

java - Gradle 实现未将 gradle 模块导入 gradle 项目

graphql - Apollo GraphQL 错误 : Query root type must be provided

sequelize.js - GraphQL 和 graphql-sequelize-schema-generator

java - Spring Boot 启动器依赖项是否已准备好生产?