What is GraphQL and how does it differ from REST in Node.js?
Answer
GraphQL is a query language for APIs and a runtime for executing those queries, developed by Facebook. Unlike REST which has fixed endpoints returning fixed data shapes, GraphQL exposes a single endpoint (/graphql) where clients specify exactly what data they need. Key differences: (1) Over-fetching: REST often returns more data than needed; GraphQL returns exactly what was requested; (2) Under-fetching: REST may require multiple requests for related data; GraphQL fetches all in one request; (3) Schema: GraphQL has a strongly typed schema that serves as a contract; (4) Versioning: REST often needs /v1, /v2; GraphQL evolves by adding fields (deprecated ones remain). Node.js implementations: Apollo Server (most popular), graphql-yoga, Mercurius (for Fastify). Best suited for: complex data graphs, multiple clients (mobile/web) with different data needs, rapid product iteration. REST is still better for simple CRUD, file uploads, and when HTTP caching is important.
Previous
What is Redis and how is it used in Node.js applications?
Next
What is WebSocket and how do you implement it in Node.js?