Guru Cse
Guru Cse

Reputation: 3275

GraphQl: Object type "xxx" implements a known interface, but no class could be found for that type name

GraphQl and spring boot.

Graph ql schema

type Query {
    animal(id: ID!): Animal!
}

interface Animal{ 
    id: ID
    name: String
}

type Cat implements Animal {
   id: ID
   name: String
}

type Dog implements Animal {
   id: ID
   name: String
}


fun animal(id: String): CompletableFuture<Any> {
    return service.getAnimal(id).toFuture();
}

Error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schemaParser' defined in class path resource [graphql/kickstart/tools/boot/GraphQLJavaToolsAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.coxautodev.graphql.tools.SchemaParser]: Factory method 'schemaParser' threw exception; nested exception is com.coxautodev.graphql.tools.SchemaClassScannerError: Object type 'Cat' implements a known interface, but no class could be found for that type name. Please pass a class for type 'Cat' in the parser's dictionary.

Upvotes: 4

Views: 1705

Answers (1)

Guru Cse
Guru Cse

Reputation: 3275

Two solution(kotlin)

Solution 1: Add a config file to your project and add the classes to dictionary

@Configuration
class GraphQLConfig {

@Bean
@Primary
fun schemaParserDictionary(): SchemaParserDictionary? {
    val dictionary = SchemaParserDictionary()
    dictionary.add("Cat", Cat::class.java)
    dictionary.add("Dog", Dog::class.java)
    return dictionary
  }
}

Solution 2: Add resolvers for each type and modify the schema

  • add dummy resolvers

    fun cat(): Cat? {
     return null
    }
    
    fun dog(): Dog? {
     return null
    }
    
  • also change the schema

Before

type Query {
    animal(id: ID!): Animal!
}

After

type Query {
    animal(id: ID!): Animal!
    cat
    dog
}

Upvotes: 2

Related Questions