question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassCastException: graphql.schema.GraphQLObjectType cannot be cast to graphql.schema.GraphQLInputType

See original GitHub issue

We 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danielocampo2commented, Jun 21, 2017

@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!

0reactions
andimarekcommented, Apr 30, 2017

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found