GraphQL Integration
JustAPI supports running GraphQL alongside REST endpoints in the same application.
Basic Setup
Section titled “Basic Setup”Install Strawberry:
pip install strawberry-graphqlCreate your GraphQL schema:
import strawberryfrom justapi import JustAPIApp
@strawberry.typeclass User: id: int name: str email: str
@strawberry.typeclass Query: @strawberry.field def users(self) -> list[User]: return [User(id=1, name="Alice", email="alice@example.com")]
@strawberry.typeclass Mutation: @strawberry.mutation def create_user(self, name: str, email: str) -> User: return User(id=2, name=name, email=email)Mount the GraphQL endpoint in your JustAPI app:
import strawberryfrom strawberry.fastapi import GraphQLRouter
app = JustAPIApp()
schema = strawberry.Schema(query=Query, mutation=Mutation)graphql_app = GraphQLRouter(schema)
app.include_router(graphql_app, prefix="/graphql")Testing GraphQL
Section titled “Testing GraphQL”Start your app and visit http://localhost:8000/graphql for the GraphiQL interactive explorer.
query { users { id name email }}Shared Authentication
Section titled “Shared Authentication”JustAPI supports global dependencies that apply to all routes including GraphQL:
from justapi import Depends, Security
def get_current_user(request): token = request.headers.get("Authorization", "").replace("Bearer ", "") # validate token... return {"user_id": 1, "name": "Alice"}
app = JustAPIApp(dependencies=[Depends(get_current_user)])- Install
strawberry-graphqlfor GraphQL support. - Mount
GraphQLRouterwith a prefix usingapp.include_router(). - GraphQL runs alongside REST — no separate server needed.
- Use JustAPI’s dependency injection for shared auth.
See Also
Section titled “See Also”- Routing & Sub-routers — multiple routers
- Security — authentication