'Expected type 'Upload' to be a GraphQLInputType Was a type only permitted for object types incorrectly used as an input type, or vice-versa

Here i'm trying to receive Upload file in Graphql. My Code as follows

Graphql schema example.graphqls

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

input CreateFileUploadInput {
    files: Upload
    id: String
}

Graphql Scalar upload defined in 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 configurer 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());
  }
}

my Model class CreateFileUploadInput.java

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

I'm using graphql spring boot version in build.gradle is 5.8.1 and gradle gradle-5.6.2-bin

Im getting below Exception while i run my Spring Boot application!

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]

I have been spending lot of times to investigate this issue, still i could't solve it. Pls help!



Solution 1:[1]

I was having the same problem over here and was able to solve it (be it not in a very nice way) after reading something mentioned on https://github.com/graphql-java-kickstart/graphql-java-tools/issues/77.

It states that by using the type explicitly in the root of a query/mutation, it will also be available on a nested level. In your case this would mean adding an additional method e.g. typLoaderDummy, as shown in the sample below (which will not actually be used) to your definition. Keep in mind that you will need to provide an implementation for this "dummy" method on your Java implementation.

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

For me this workaround solved the problem.

Solution 2:[2]

I encountered this error when I tried to use a type rather than input as an input, which isn't allowed. This is slightly different from the original question, but you'll see the same error:

So this was NOT working:

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

type CreateFileUploadInput {
    files: Upload
    id: String
}

This fixed it:

input CreateFileUploadInput {
    files: Upload
    id: String
}

(Just changed it from type to input)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 P.V.M. Kessels
Solution 2 Joey