click to drop food
4
Extend
Import resolvers and schemas from external packages
1 min readEdit on GitHub
Extend
Extend Nitro GraphQL with resolvers and schemas from external packages. This feature is designed for code sharing in monorepo structures, creating npm packages, and modular GraphQL architecture.
Quick Start
TypeScript
import graphql from 'nitro-graphql'
export default defineNitroConfig({
modules: [
graphql({
extend: ['@myorg/graphql', './packages/shared-graphql']
})
]
})
Each path automatically appends /resolvers and /schema:
| Input | Imported |
|---|---|
@myorg/graphql | @myorg/graphql/resolvers + @myorg/graphql/schema |
./packages/shared | ./packages/shared/resolvers + ./packages/shared/schema |
Package Structure
The extend package must export in a specific format:
resolvers.ts
TypeScript
// Nitro-compatible format: array of { resolver: ResolverObject }
export const resolvers = [
{
resolver: {
Query: {
users: () => db.users.findMany(),
user: (_, { id }) => db.users.findById(id)
}
}
},
{
resolver: {
Mutation: {
createUser: (_, { input }) => db.users.create(input),
deleteUser: (_, { id }) => db.users.delete(id)
}
}
}
]
schema.ts
TypeScript
// schemaString export is required
export const schemaString = `
type Query {
users: [User!]!
user(id: ID!): User
}
type User {
id: ID!
name: String!
email: String!
}
`
import { parse } from 'graphql'
export const typeDefs = parse(schemaString)
package.json
JSON
{
"name": "@myorg/graphql",
"type": "module",
"exports": {
".": "./index.ts",
"./resolvers": "./resolvers.ts",
"./schema": "./schema.ts"
}
}
Extend-Only Usage
To ignore local files:
TypeScript
graphql({
extend: ['@myorg/complete-api'],
skipLocalScan: true // server/graphql/ won't be scanned
})
Info
Use skipLocalScan: true if your extend package contains the complete API and you don't need local files.
Contributors
Comments
Use ← → arrow keys to navigate