graphql-java-tools icon indicating copy to clipboard operation
graphql-java-tools copied to clipboard

Custom scalars are not properly scanned when only used in input types

Open mb-nick opened this issue 5 years ago • 3 comments

I have some trouble with custom scalars that are only used in input types. I used the [https://github.com/graphql-java-kickstart/graphql-java-tools/tree/master/example](example from this repository) to recreate this problem. I've just added a scalar to the existing input type,

Schema:

scalar Foo

input CreateHumanInput {
    # The name of the human
    name: String
    # The home planet of the human, or null if unknown
    homePlanet: String
    # custom scalar
    foo: Foo
}

Spring Config

@Bean
    public GraphQLScalarType foo() {
        return GraphQLScalarType.newScalar(Scalars.GraphQLString)
                .name("Foo")
                .description("foo")
                .build();
    }

When I start the application I get the following exception (that's just the root cause)

Caused by: com.coxautodev.graphql.tools.SchemaError: Expected type 'Foo' 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:331) ~[graphql-java-tools-5.5.0.jar:na]
	at com.coxautodev.graphql.tools.SchemaParser.determineInputType(SchemaParser.kt:319) ~[graphql-java-tools-5.5.0.jar:na]
	at com.coxautodev.graphql.tools.SchemaParser.createInputObject(SchemaParser.kt:196) ~[graphql-java-tools-5.5.0.jar:na]
	at com.coxautodev.graphql.tools.SchemaParser.parseSchemaObjects(SchemaParser.kt:105) ~[graphql-java-tools-5.5.0.jar:na]
	at com.coxautodev.graphql.tools.SchemaParser.makeExecutableSchema(SchemaParser.kt:130) ~[graphql-java-tools-5.5.0.jar:na]
	at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration.graphQLSchema(GraphQLJavaToolsAutoConfiguration.java:116) ~[graphql-spring-boot-autoconfigure-5.5.0.jar:na]
	at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$bd848caf.CGLIB$graphQLSchema$1(<generated>) ~[graphql-spring-boot-autoconfigure-5.5.0.jar:na]
	at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$bd848caf$$FastClassBySpringCGLIB$$2867ec26.invoke(<generated>) ~[graphql-spring-boot-autoconfigure-5.5.0.jar:na]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.11.RELEASE.jar:5.1.11.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.11.RELEASE.jar:5.1.11.RELEASE]
	at com.oembedler.moon.graphql.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$bd848caf.graphQLSchema(<generated>) ~[graphql-spring-boot-autoconfigure-5.5.0.jar:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.11.RELEASE.jar:5.1.11.RELEASE]
	... 90 common frames omitted

In addition to the exception there is also a warning as one of the first log statements

Cannot find definition for field 'foo: Foo' on input type 'CreateHumanInput' -> java.util.Map.  or add a class to represent your input type instead of a Map.

The example in the repository uses version 5.5.0. I also tried version 5.7.1, which I use in the actual project. But that didn't change anything.

I also tried it with an explicitly defined SchemaParser.

@Bean
    public SchemaParser schemaParser(GraphQLScalarType foo, Query query, Mutation mutation) {
        return SchemaParser.newParser()
                .file("swapi.graphqls")
                .scalars(foo)
                .resolvers(query, mutation)
                .build();
    }

When I add the scalar to a normal (output) type, then it works. Although the above mentioned warning stays.

Is this a bug (I guess so) or did I do something wrong? Is there a workaround? Any help is appreciated.

mb-nick avatar Jan 21 '20 12:01 mb-nick

Did you find a solution to this? I am facing the same issue

ubarua123 avatar Feb 13 '20 11:02 ubarua123

This looks like a bug. We would welcome a PR that would fix this.

vojtapol avatar Feb 13 '20 13:02 vojtapol

~~Similar issue here, but with enums, not scalars.~~

The problem I had with enums turned out to be a field naming mismatch. Pilot error of a sort. So please disregard the above comment.

tedepstein avatar Sep 08 '20 14:09 tedepstein