Describe the bug
When trying to generate types for schema, using an empty Query type then importing another file that includes an extension Query leads to an error saying that Query must define one or more fields.
To Reproduce
Steps to reproduce the behavior:
GraphQL schema (schema.graphql):
# import * from "users.graphql"
type Query
Inside users.graphql:
extend type Query {
users: [User!]!
}
type User {
name: String!
}
codegen.yml config file:schema: api/graphql/schema.graphql
generates:
common/types/schema.ts:
plugins:
- typescript
- typescript-resolvers
Expected behavior
Since users.graphql extends Query I was expecting the type generation to work as expected, but it's telling me Query needs one or more fields. Is this a graphql-import issue? I've tried different import variants (#import "users.graphql", #import User from "users.graphql") to no avail.
Environment:
@graphql-codegen/...: 1.11.2@Li357 Empty object definitions aren't allowed in GraphQL SDL.
An Object type must define one or more fields.
http://spec.graphql.org/draft/#sec-Objects.Type-Validation
@ardatan Got it. So is there a way to accomplish this modularization? Or would I have to add a dummy field to Query? I really don't want to do that鈥揺specially if there's not a way to hide it from public consumers of the API.
@Li357 You might not need extend at all. Just define types as they are.
@ardatan If I remove the empty type, graphql-codegen won't find any schema to process, i.e. it gives me Unable to find any GraphQL type definitions. I want to split up different parts of my schema into different files then join them together into one, something the docs explicitly mentions.
Looks like schema.graphql only imports users.graphql if I explicitly reference something from it, but I was hoping for side-effects, i.e. the declaration of of Query.
@Li357 can you please share a reproduction for this?
You should be able to create multiple .graphql files, each one with types, without the need to deal with imports at all, and codegen will merge the types for you. (no need for extend or #import)
@dotansimha IIRC, I had multiple graphql files and I specified a glob pattern in my codegen.yml. But I needed to specify a single file for Apollo. I'll get back when I have a chance to verify.
This looks something related to GraphQL Import. You can use GraphQL Toolkit instead of import. It has a feature for scanning graphql files for you instead of connecting them each other. So you can use glob expressions everywhere instead of #import
Most helpful comment
This looks something related to GraphQL Import. You can use GraphQL Toolkit instead of import. It has a feature for scanning graphql files for you instead of connecting them each other. So you can use glob expressions everywhere instead of #import