notes

package
v0.21.30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package notes is the canonical AsCRUD demo: an entire module surface — five REST endpoints + five GraphQL ops, with paging, validation, and a per-request store — boils down to a single `nexus.AsCRUD[Note](...)` line.

The resolver takes fx-injected deps after ctx, so the user's DB (or DBM, cache, queue, …) drops in just by appearing in the signature. Any dep that implements NexusResources() — the framework's resource provider interface — is automatically linked to the generated endpoints on the dashboard's architecture canvas: no manual edge declarations.

Index

Constants

This section is empty.

Variables

View Source
var Module = nexus.Module("notes",
	nexus.Provide(NewService, NewNotesDB),
	nexus.AsCRUD[Note](

		func(ctx context.Context, db *DB) (nexus.Store[Note], error) {
			return db.store, nil
		},
		nexus.WithGraphQL(),
		nexus.WithoutREST(),
	),
)

Module wires the entire CRUD surface in a single declaration. Compare with users/module.go: that module hand-writes Get/List/ Create/Search across REST and GraphQL; this one declares the shape and lets the framework generate everything.

REST endpoints generated by default:

GET    /notes              → list (with ?limit / ?offset / ?sort)
GET    /notes/:id          → read
POST   /notes              → create
PATCH  /notes/:id          → update (shallow merge)
DELETE /notes/:id          → delete

GraphQL is opt-in — pass nexus.WithGraphQL() to expose:

query    listNotes(limit, offset, sort)
query    getNote(id)
mutation createNote(...)
mutation updateNote(id, ...)
mutation deleteNote(id) → Boolean

To go GraphQL-only, pair WithGraphQL() with nexus.WithoutREST().

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB stands in for the user's real DB wrapper (DBM, *gorm.DB, whatever). It implements NexusResourceProvider so the framework registers a Resource node for it on the dashboard, and AsCRUD's auto-attach pass links every generated endpoint to that resource.

func NewNotesDB

func NewNotesDB() *DB

NewNotesDB builds the DB wrapper around an in-memory store. In a real app this would dial Postgres or open a GORM session.

func (*DB) NexusResources

func (d *DB) NexusResources() []resource.Resource

NexusResources is what tells the framework "this dep is a resource the dashboard should know about." AsCRUD's resolver walker picks this up and links the resource to every generated endpoint — canvas edges and per-row chips both light up automatically.

type Note

type Note struct {
	ID    string `json:"id"`
	Title string `json:"title" validate:"required,len=1|120"`
	Body  string `json:"body"`
}

Note is the resource type. AsCRUD reflects on it to:

  • Generate URL paths from the lowercased pluralized type name ("Note" → "/notes").
  • Locate the "ID" field as the primary key for the in-memory store. Any exported `string`-typed field literally named ID works; rename or pass explicit accessors otherwise.
  • Build the GraphQL input/output types from the struct fields.

Validation tags (validate:"...") flow through the same path as every other nexus handler — invalid Create/Update bodies surface as 400s on REST and as resolver errors on GraphQL.

type Service

type Service struct{ *nexus.Service }

Service exists purely so the framework's GraphQL auto-mount has a service to attach the generated ops onto. AsCRUD doesn't need any behaviour from it — the per-request resolver does the real work.

In a real module you'd add cross-module methods on Service for other modules to call (the shadow generator picks those up); the CRUD surface itself stays generated.

func NewService

func NewService(app *nexus.App) *Service

NewService is fx-resolved at boot. The Describe text shows up on the dashboard's module card, so this is the right place to give human reviewers a one-line summary of the module.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL