ClassCastException: graphql.schema.GraphQLObjectType cannot be cast to graphql.schema.GraphQLInputType
See original GitHub issueWe are using same schema for Query and Mutation. While generation of schema it gives below error,
Exception in thread "main" java.lang.ClassCastException: graphql.schema.GraphQLObjectType cannot be cast to graphql.schema.GraphQLInputType
at graphql.schema.GraphQLArgument.replaceTypeReferences(GraphQLArgument.java:30)
at graphql.schema.SchemaUtil.resolveTypeReferencesForFieldsContainer(SchemaUtil.java:153)
at graphql.schema.SchemaUtil.replaceTypeReferences(SchemaUtil.java:141)
at graphql.schema.GraphQLSchema$Builder.build(GraphQLSchema.java:110)
at graphql.schema.GraphQLSchema$Builder.build(GraphQLSchema.java:104)
at com.opshub.oim.graphql.common.GraphQLConstants.main(GraphQLConstants.java:107)
Here is the main program to reproduce the issue,
public static void main(final String[] args) {
final GraphQLInputObjectType attributeListInputObjectType = GraphQLInputObjectType.newInputObject().name("attributes")
.description("attribute")
.field(GraphQLInputObjectField.newInputObjectField().type(GraphQLString).name("key").build())
.field(GraphQLInputObjectField.newInputObjectField().type(GraphQLString).name("value").build()).build();
final GraphQLObjectType attributeListObjectType = GraphQLObjectType.newObject().name("attributes").description("attribute")
.field(newFieldDefinition().type(GraphQLString).name("key").build())
.field(newFieldDefinition().type(GraphQLString).name("value").build()).build();
final GraphQLObjectType systemForMutation = GraphQLObjectType.newObject().name("systems").description("systems")
.field(newFieldDefinition().name("attributes").type(new GraphQLList(attributeListInputObjectType)).build())
.field(newFieldDefinition().name("attributes1").type(attributeListObjectType).build())
.field(newFieldDefinition().type(new GraphQLTypeReference("systems")).name("parentSystem").build())
.build();
final GraphQLObjectType systemForQuery = GraphQLObjectType.newObject().name("systems").description("systems")
.field(newFieldDefinition().name("attributes").type(new GraphQLList(attributeListObjectType)).build())
.field(newFieldDefinition().name("attributes1").type(attributeListObjectType).build())
.field(newFieldDefinition().type(new GraphQLTypeReference("systems")).name("parentSystem").build())
.build();
final GraphQLFieldDefinition systemWithArgsForMutation = newFieldDefinition().name("systems").type(systemForMutation)
.dataFetcher(new SystemDataFetcher())
.argument(GraphQLArgument.newArgument().name("attributes").type(new GraphQLList(attributeListInputObjectType)).build())
.argument(GraphQLArgument.newArgument().name("attributes1").type(attributeListInputObjectType).build()).build();
final GraphQLFieldDefinition systemWithArgsForQuery = newFieldDefinition().name("systems").type(systemForQuery)
.dataFetcher(new SystemDataFetcher())
.argument(GraphQLArgument.newArgument().name("attributes").type(new GraphQLList(attributeListObjectType)).build())
.argument(GraphQLArgument.newArgument().name("attributes1").type(attributeListInputObjectType).build()).build();
final GraphQLObjectType queryType = GraphQLObjectType.newObject().name("query").field(systemWithArgsForQuery).build();
final GraphQLObjectType mutation = GraphQLObjectType.newObject().name("mutation").field(systemWithArgsForMutation).build();
final GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).mutation(mutation).build();
}
Looks like, GraphQL use internal schema caching for resolveTypeReference method.
I think, If we change SchemaUtil.java below line or condition with and then it should work,
GraphQLType resolveTypeReference(GraphQLType type, Map<String, GraphQLType> typeMap)
/* */ {
/* 165 */ if ((type instanceof GraphQLTypeReference) **||** (typeMap.containsKey(type.getName()))) {
Regards, OpsHub Inc.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Can't have Input Types with nested fields in GraphQL. Error
I just realized I had inconsistency in my schema.graphqls . Schema says: input ProductOrderDetailsInput { orderUnits: [OrderUnitInput] ...
Read more >[DO NOT MERGE] Fix input type as a list of object. — Bitbucket
lang.ClassCastException: graphql.schema.GraphQLObjectType cannot be cast to graphql.schema.GraphQLInputType 2confluence_1 | at graphql.
Read more >graphql/type
The graphql/type module is responsible for defining GraphQL types and schema. You can import either from the graphql/type module, or from the root...
Read more >graphql.schema.GraphQLTypeReference Java Examples
This page shows Java code examples of graphql.schema.GraphQLTypeReference. ... toList()); GraphQLObjectType edge = buildContext.relay.
Read more >GraphQL schema basics - Apollo GraphQL Docs
Your GraphQL server uses a schema to describe the shape of your available data. ... If ! appears inside the square brackets, the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@andimarek but does this limitation applies to any object in your schema? If we have 2 different queries with a parameter, say, “userID” then that means that we can’t use that same parameter name even if it represents exactly the same data for both queries? Thanks!
@opshubdata As @bbakerman mentioned you can’t have to types with the same name: but you used
attributes1
two times. We will improve the error message, but this will not fix your problem: you have to use unique type names.