Strawberry: Type Query must define one or more fields

This error occurs when you pass a Strawberry type to strawberry.Schema as the query parameter, but that type doesn’t define any fields. To fix this, define at least one field on the type you passed as the query argument.

I came across this error when working on the real-time chat app GraphQL example. Since I only needed subscriptions and no queries in the project, I tried not passing any query to strawberry.Schema. That got me the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/usr/local/lib/python3.9/runpy.py", line 288, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/local/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/opt/container/main.py", line 20, in <module>
    schema = strawberry.Schema(subscription=Subscription, mutation=Mutation)
TypeError: __init__() missing 1 required positional argument: 'query'

Let’s take a look at what I tried to do to fix this and why that didn’t work.

The Problem: You didn’t define at least one field in your query type

Below is where we defined our Strawberry schema:

import os
import uvicorn
import strawberry
from strawberry.fastapi import GraphQLRouter
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware

from gql import Query, Subscription, Mutation

app = FastAPI()

app.add_middleware(
            CORSMiddleware,
            allow_origins=[f"http://{os.getenv('HOSTNAME')}:3000"],
            allow_credentials=True,
            allow_methods=["*"],
            allow_headers=["*"]
        )

schema = strawberry.Schema(query=Query, subscription=Subscription, mutation=Mutation)
app.include_router(GraphQLRouter(schema), prefix="/graphql")

if __name__ == "__main__":
    prod = os.getenv("PROD") == "1"
    uvicorn.run("main:app", host="0.0.0.0", reload=not prod)
Python

And here is how I tried to fix the issue of having to define a query parameter to strawberry.Schema without actually using it:

room = Room(users=[], messages=[])

@strawberry.type
class Query:
    pass

@strawberry.type
Python

And that resulted in the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/usr/local/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/usr/local/lib/python3.9/runpy.py", line 288, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/local/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/opt/container/main.py", line 20, in <module>
    schema = strawberry.Schema(query=Query, subscription=Subscription, mutation=Mutation)
  File "/usr/local/lib/python3.9/site-packages/strawberry/schema/schema.py", line 150, in __init__
    raise ValueError(f"Invalid Schema. Errors:\n\n{formatted_errors}")
ValueError: Invalid Schema. Errors:

❌ Type Query must define one or more fields.

So, not only do we need to have a query type defined in the schema, we also need to have at least one field defined in that type.

The Solution: Add at least one field to your query type

The solution was simple enough. Instead of just returning the room object directly, I put it as a field in the query type and just called Query.get_room wherever I needed to return the room object.

room = Room(users=[], messages=[])

@strawberry.type
class Query:
    @strawberry.field
    async def get_room() -> Room:
        return room

@strawberry.type
class Subscription:
Python

Kind of weird, not sure why this is required, but this is how you fix it.