Describe the bug
When I create an app that has no queries, but only mutations, an error is thrown.
To Reproduce
Create a project with this simple reducer:
@Resolver()
class MyResolver{
@Mutation(returns => Boolean)
myMutation(){
return true;
}
Then try to build the schema:
const schema: GraphQLSchema = await buildSchema({
resolvers: [MyResolver]
});
Expected behavior
I would expect this to work, as I don't see a reason why having at least one query should be mandatory.
Logs
Stack trace:
{ Error: Generating schema error
at Function.<anonymous> (/my/project/node_modules/type-graphql/dist/schema/schema-generator.js:19:23)
at Generator.next (<anonymous>)
at fulfilled (/my/project/node_modules/tslib/tslib.js:104:62)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
details:
[ { Type Query must define one or more fields.
at SchemaValidationContext.reportError (/my/project/node_modules/graphql/type/validate.js:99:19)
at validateFields (/my/project/node_modules/graphql/type/validate.js:318:13)
at validateTypes (/my/project/node_modules/graphql/type/validate.js:281:9)
at validateSchema (/my/project/node_modules/graphql/type/validate.js:63:3)
at graphqlImpl (/my/project/node_modules/graphql/graphql.js:57:61)
at /my/project/node_modules/graphql/graphql.js:32:250
at new Promise (<anonymous>)
at Object.graphql (/my/project/node_modules/graphql/graphql.js:30:10)
at Function.<anonymous> (/my/project/node_modules/type-graphql/dist/schema/schema-generator.js:17:48)
at Generator.next (<anonymous>) message: 'Type Query must define one or more fields.' } ] }
Environment
Type Query must define one or more fields means that one of your method must be decorated with @Query().
It's invalid to create a schema without a query type or a type without any fields. It's forced by GraphQL spec:
The query root operation type must be provided and must be an Object type.
@MichalLytek I have a use case where I have one resolver with only one mutation. And while doing testing it fails as I'm only loading this resolver. Using Nestjs btw.
@MichalLytek I have a resolver that extends an @InterfaceType which has all the (common) @Query decorators. But I'm still getting the above error.
The interface:
@InterfaceType()
export abstract class IScopedResolver {
@Query(returns => Contact, { nullable: true })
async contact(...)
//...
The resolver:
@Resolver(of => Contact)
export class ContactResolver extends IScopedResolver {
// currently empty
}
I was referring to resolver-inheritance docs for this. What am I doing wrong here?
Let me know if I need to open a new issue.
@udayrajMT @InterfaceType() is not equal to @Resolver(). Please read the manual again from top to bottom before trying to make some weird patterns.
@MichalLytek right, I don't know what I was thinking - mixed up two examples I guess.
I hadn't created an interface since there were no @Fields to add to it.
Now I've created an empty one.
@InterfaceType()
export default abstract class IScoped {
}
And changed the resolver interface as below-
@Resolver(of => IScoped, { isAbstract: true })
export abstract class IScopedResolver {
// ... queries
}
Yet to test this completely, but thanks anyway for the quick reply and clarification!
Most helpful comment
Type Query must define one or more fieldsmeans that one of your method must be decorated with@Query().It's invalid to create a schema without a query type or a type without any fields. It's forced by GraphQL spec:
https://graphql.github.io/graphql-spec/June2018/#sec-Schema