GraphQL schema and resolvers
Local baseline: Node.js 22.16.0, ECMAScript modules, synthetic data. Run each complete example independently as node main.mjs in a disposable directory. Static ByteMind blocks do not imply live Node execution. SQLite is experimental in this baseline. Third-party integrations below are staged candidates, not installed or validated dependencies.
Resolve a book by identifier
const books = [{id:'b1', title:'River Atlas', pages:120}];function book(parent, {id}, context) { return context.books.find(row => row.id === id) ?? null;}console.log(JSON.stringify(book(null,{id:'b1'},{books})));console.log(book(null,{id:'missing'},{books}));Expected output:
{"id":"b1","title":"River Atlas","pages":120}nullThe resolver returns one matching record or null. It does not interpret query syntax.
How it works
A schema defines object types, scalar values such as ID and Int, input types and enums. Query is the read entry point; Mutation is the conventional write entry point. Variables carry values separately from operation text. A non-null field (marked !) cannot return null successfully: an execution error propagates to the nearest nullable ancestor. A nullable missing book may legitimately be null. An execution response may contain both data and errors. Subscriptions produce a stream of results and require a separately selected transport. GraphQL does not require a particular database or replace authorization.
Validate a mutation domain input
function addBook(rows, input) { if (!input.title.trim() || !Number.isInteger(input.pages) || input.pages < 1) throw new Error('Invalid book'); return [...rows, {id:'b2',title:input.title.trim(),pages:input.pages}];}const rows=[];console.log(addBook(rows,{title:' Field Notes ',pages:48})[0].title);try { addBook(rows,{title:'Notes',pages:0}); } catch(e) { console.log(e.message); }console.log(rows.length);Expected output:
Field NotesInvalid book0A valid mutation input produces a new array; invalid page count is rejected. This is domain logic, not GraphQL input coercion.
GraphQL.js catalog execution
NOT RUN — graphql (16.x candidate; exact reviewed version not installed). This integration requires reviewed pinned dependencies and a disposable fixture. Do not execute against shared services.
import {buildSchema, graphql} from 'graphql';const schema = buildSchema(` type Book { id: ID!, title: String!, pages: Int! } type Query { book(id: ID!): Book }`);const rootValue = {book: ({id}) => id === 'b1' ? {id, title:'River Atlas',pages:120} : null};const result = await graphql({schema,rootValue,source:'query ($id: ID!) { book(id: $id) { title } }',variableValues:{id:'b1'}});console.log(JSON.stringify(result));Intended result (not observed): {"data":{"book":{"title":"River Atlas"}}}
Common mistakes
Do not concatenate untrusted input into operation strings. Never interpret a typed schema as an access policy. Do not equate resolver tests with schema validation, HTTP status or GraphQL execution.
Practice and next steps
Extend the fictional book catalog with a nullable subtitle and predict what a client requesting a missing book should receive. In a reviewed GraphQL installation, add a query with an unknown field and assert validation failure before execution.
Further reading: https://graphql.org/learn/execution/. Related staged reference: node-graphql-schema-and-resolvers-lookup (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
GraphQL schema and resolvers — knowledge check
Apply the lesson to a fresh scenario and identify the failure boundary.
What determines which fields a GraphQL response includes?
A nullable book field has no matching record. What can a resolver return?
What does calling a resolver directly verify?