🟢 Node.js Intermediate

What is GraphQL and how does it differ from REST in Node.js?

Why Interviewers Ask This

Mid-level Node.js roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

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.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Node.js project, I used this when...' immediately makes your answer more credible and memorable.