What is GraphQL, and how does it differ from REST?

GraphQL is a query language for APIs that allows clients to request only the data they need, unlike REST, which provides fixed endpoints for accessing resources. GraphQL enables better flexibility and efficiency in data retrieval, especially for complex applications with diverse data requirements. Example of a simple GraphQL server using Apollo Server:

const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello, world!',
  },
};

const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});