GraphQL
Overview
GraphQL is an open-source query language for APIs plus a runtime that fulfills those queries against a typed schema. Clients ask for exactly the fields they need in one request, eliminating over- and under-fetching common to REST. It is transport- and database-agnostic — resolvers fetch data from any backend. Read this when designing a schema, writing resolvers, or choosing a GraphQL server.
Official sources
Install / setup
# Reference JavaScript implementation:
npm install graphql --save
Source: https://www.graphql-js.org/docs/getting-started/ (graphql-js is the official JS implementation: parser, type system, validator, executor).
Core concepts
- Schema (SDL) — the typed contract;
type, Query, Mutation, Subscription define the API surface.
- Types — scalars, objects, enums, interfaces, unions, input types, and non-null/list modifiers.
- Operations — queries (read), mutations (write), subscriptions (real-time streams).
- Resolvers — functions that return the data for each field; the heart of execution.
- Fields & arguments — clients select fields and pass typed arguments per field.
- Introspection — the schema is self-describing; tools like GraphiQL query it.
- Single endpoint — typically one POST endpoint (e.g.
/graphql) for all operations.
- Fragments & variables — reusable field sets and parameterized, cacheable queries.
Best practices
Common pitfalls
- N+1 queries when a list resolver hits the DB per item → batch with DataLoader.
- Unbounded query depth/complexity enables DoS → add depth/complexity limits and timeouts.
- Exposing a single
/graphql makes per-resource HTTP caching hard → use persisted queries / response caching.
Examples
import { graphql, buildSchema } from 'graphql';
const schema = buildSchema(`type Query { hello: String }`);
const rootValue = { hello: () => 'Hello world!' };
graphql({ schema, source: '{ hello }', rootValue })
.then((res) => console.log(res));
// { data: { hello: 'Hello world!' } }
Further reading
Related skills
- ../spring — expose a GraphQL API from a Spring backend (Spring for GraphQL)
- ../nginx — reverse-proxy a GraphQL server behind Nginx
1---2name: graphql3description: GraphQL — a query language and server-side runtime for APIs where clients request exactly the fields they need from a strongly-typed schema. Consult when designing a schema (SDL), writing queries/mutations/subscriptions, building a resolver-based server (graphql-js, Apollo), avoiding over/under-fetching, or referencing the GraphQL specification.4domain: stack5category: backend6tags: [graphql, api, schema, resolvers, query-language, graphql-js, backend]7official_sources:8 - https://graphql.org/learn/9 - https://github.com/graphql/graphql-spec10 - https://www.graphql-js.org/docs/getting-started/11verified: 2026-06-1712metadata:13 source: https://skillmd.com/skills/gabriel7silva/graphql14---1516# GraphQL1718## Overview19GraphQL is an open-source query language for APIs plus a runtime that fulfills those queries against a typed schema. Clients ask for exactly the fields they need in one request, eliminating over- and under-fetching common to REST. It is transport- and database-agnostic — resolvers fetch data from any backend. Read this when designing a schema, writing resolvers, or choosing a GraphQL server.2021## Official sources22- Docs: https://graphql.org/learn/23- Repo: https://github.com/graphql/graphql-spec24- Install: https://www.graphql-js.org/docs/getting-started/2526## Install / setup27```bash28# Reference JavaScript implementation:29npm install graphql --save30```31Source: https://www.graphql-js.org/docs/getting-started/ (graphql-js is the official JS implementation: parser, type system, validator, executor).3233## Core concepts34- **Schema (SDL)** — the typed contract; `type`, `Query`, `Mutation`, `Subscription` define the API surface.35- **Types** — scalars, objects, enums, interfaces, unions, input types, and non-null/list modifiers.36- **Operations** — queries (read), mutations (write), subscriptions (real-time streams).37- **Resolvers** — functions that return the data for each field; the heart of execution.38- **Fields & arguments** — clients select fields and pass typed arguments per field.39- **Introspection** — the schema is self-describing; tools like GraphiQL query it.40- **Single endpoint** — typically one POST endpoint (e.g. `/graphql`) for all operations.41- **Fragments & variables** — reusable field sets and parameterized, cacheable queries.4243## Best practices44- Design the schema around client/product needs, not your database tables (https://graphql.org/learn/schema/).45- Solve N+1 resolver fan-out with batching/caching (DataLoader) (https://graphql.org/learn/best-practices/).46- Use cursor-based (connections) pagination for lists (https://graphql.org/learn/pagination/).47- Return structured `errors` and prefer nullable fields for partial failures (https://graphql.org/learn/best-practices/).4849## Common pitfalls50- N+1 queries when a list resolver hits the DB per item → batch with DataLoader.51- Unbounded query depth/complexity enables DoS → add depth/complexity limits and timeouts.52- Exposing a single `/graphql` makes per-resource HTTP caching hard → use persisted queries / response caching.5354## Examples55```javascript56import { graphql, buildSchema } from 'graphql';5758const schema = buildSchema(`type Query { hello: String }`);59const rootValue = { hello: () => 'Hello world!' };6061graphql({ schema, source: '{ hello }', rootValue })62 .then((res) => console.log(res));63// { data: { hello: 'Hello world!' } }64```6566## Further reading67- https://spec.graphql.org/ — the formal GraphQL specification versions68- https://graphql.org/community/tools-and-libraries/ — servers and clients per language6970## Related skills71- ../spring — expose a GraphQL API from a Spring backend (Spring for GraphQL)72- ../nginx — reverse-proxy a GraphQL server behind Nginx