graphql-java-tools
graphql-java-tools copied to clipboard
Nested Input Types not Working for Mutations
Hi, I am getting the nested type not being recognized error when using this on a mutation.
Schema ` type Query { hero(episode: Episode): Character human(id: String!): Human droid(id: String!): Droid character(id: String!): Character heroes: [Character!]! }
enum Episode { NEWHOPE, EMPIRE, JEDI }
interface Character { id: String! name: String friends: [Character] appearsIn: [Episode] }
type Human implements Character { id: String! name: String friends: [Character] appearsIn: [Episode] homePlanet: String }
type Droid implements Character { id: String! name: String friends: [Character] appearsIn: [Episode] primaryFunction: String }
type Mutation { createHuman(input: CreateHumanInput!): Human }
input FriendInput { id: String }
input CreateHumanInput { name: String! homePlanet: String! friends: [FriendInput] } `
Error
com.coxautodev.graphql.tools.SchemaError: Expected type 'FriendInput' 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?
I tried what was done in the unit test but I still get the same error.
input FriendInput { and: [FriendInput!] or: [FriendInput!] id: String }
I don't think this is limited to mutations, i'm seeing this on Queries as well. I've copied the example from https://github.com/graphql-java-kickstart/graphql-java-tools/commit/0162621251c0598a15671fcac8c181c5c7e76b1c into an example and I get a similar error:
Expected type 'RequestFilter' 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?
Can anyone share a (small) repo showing this problem? Just updated the unit test for NestedInputTypesSpec
to actually run the query with nested input types, but that one succeeds without error: https://github.com/graphql-java-kickstart/graphql-java-tools/commit/5bbc37ebcd2afeca8559eff8a5028b5401b2d744
@oliemansm
Here's a small repo that reproduces there error with ./gradlew bootRun
There's a single schema (copied from the the original unit test) and a single resolver
Thanks for the example! Strange, since the GraphQLJavaToolsAutoConfiguration
uses the same approach to build the SchemaParser
as the unit test.
Right, got it. It requires public getters for those fields. The unit test is written in Groovy, which explains why it does work since the compiler then generates the getter for us.
@oliemansm ah, great catch! I've updated the fields to public
and things are working.
I'm not quite sure if there's a good way to handle this though, perhaps the schema parser error could include some pointers on checking the visibility of those fields?
Hello @oliemansm,
I have the same errors with enums as well. Given the following schema:
` type Query { getResult (filters: InputF): [Result] }
input InputF { param1: String! param2: String! enum1: EnumA enum2: EnumB ..... }
enum EnumA { A B }
enum EnumB { A B }
`
i have implemented EnumA and EnumB as enum and InputF class with all required fields.
I tried the solution above for all fields to be public, have public getter/setter, public no-args constructor in InputF class and i get the following error:
Caused by: com.coxautodev.graphql.tools.SchemaError: Expected type 'EnumA' 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?
If i remove enum1 from InputF and add it as parameter to Query, getResult (filters: InputF, enum1: EnumA), it works for this field and i get the same exception for the rest enum fields (enum2).
Any advice?
Thanks
Just for the history for people with the same issue, add the following bean to your application with the Enums defined in the GraphQl schema
@Bean public SchemaParserDictionary schemaParserDictionary() { return new SchemaParserDictionary() .add(EnumA.class) .add(EnumB.class); }
Just for the history for people with the same issue, add the following bean to your application with the Enums defined in the GraphQl schema
@Bean public SchemaParserDictionary schemaParserDictionary() { return new SchemaParserDictionary() .add(EnumA.class) .add(EnumB.class); }
I've tried this approach with custom nested input types but it's not working
Using interface in input and encounter the same problem:
interface VisualScope {}
type VisualScope_Public implements VisualScope {
}
type VisualScope_Private implements VisualScope {
}
input Create {
scope: VisualScope!
}
type Mutation {
create(input: Create): Article
}
Error:
com.coxautodev.graphql.tools.SchemaError: Expected type 'VisualScope' 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:351) ~[graphql-java-tools-5.7.1.jar:?]
at com.coxautodev.graphql.tools.SchemaParser.determineType(SchemaParser.kt:344) ~[graphql-java-tools-5.7.1.jar:?]
Is this the same problem above?
Using interface in input and encounter the same problem:
interface VisualScope {} type VisualScope_Public implements VisualScope { } type VisualScope_Private implements VisualScope { } input Create { scope: VisualScope! } type Mutation { create(input: Create): Article }
Error:
com.coxautodev.graphql.tools.SchemaError: Expected type 'VisualScope' 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:351) ~[graphql-java-tools-5.7.1.jar:?] at com.coxautodev.graphql.tools.SchemaParser.determineType(SchemaParser.kt:344) ~[graphql-java-tools-5.7.1.jar:?]
Is this the same problem above?
You should use nestedInput in that.
For example:
input FooInput { boo: BooInput! }
input BooInput {
id: ID!
code: String!
name: String!
}
Had the same exception caused by non-public fields. The error message points in the wrong direction in this case, it's hard to infer the root of the problem from it. It probably could tell that it couldn't resolve a class by inspecting the model, and throw a more informative exception.
Closing as this appears to have been resolved since.