Standalone GraphQL service
Building a small Node.js GraphQL API without selecting an HTTP framework.
GraphQL · Class
ApolloServer is Apollo’s JavaScript GraphQL server. Construct it with schema definitions and resolvers, then run it standalone or connect it to a Node.js HTTP framework.
ApolloServer creates a GraphQL server from a schema definition and resolver map. The standalone starter attaches it to a built-in HTTP server; integrations are also available for Node.js frameworks and serverless environments.
new ApolloServer({ typeDefs, resolvers })| Name | Type | Description |
|---|---|---|
typeDefs | string | GraphQL schema written as a schema definition string. |
resolvers | object | Map of resolver functions that return field data. |
An ApolloServer instance that can be started with a standalone server or connected through a framework integration.
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
const typeDefs = `#graphql
type Query {
hello: String
}
`;
const resolvers = {
Query: { hello: () => "Hello, world!" }
};
const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server);
console.log(`Server ready at ${url}`);Defines a schema and resolver, creates Apollo Server with them, and starts its standalone HTTP server.
Building a small Node.js GraphQL API without selecting an HTTP framework.
Embedding GraphQL in an existing Node.js web application.
Improving API operations and protection.
ApolloServer is constructed with schema definitions and resolvers. The schema describes the available GraphQL operations, while resolvers provide the functions that return field data.
Follow-up: How would you adapt it to an existing web application? Use Apollo Server's framework integration API instead of relying on the built-in standalone HTTP server.
After construction, the server can be passed to startStandaloneServer, which starts Apollo Server's built-in HTTP server and returns its URL. The server can also work with Node.js HTTP frameworks through integrations.
Follow-up: What additional capabilities are documented? Apollo Server provides a plugin API, Apollo Studio integration, caching, automatic persisted queries, and CSRF prevention.
Apollo Server is a JavaScript GraphQL server that works with any Node.js HTTP framework. Its basic setup uses the @apollo/server package together with graphql.
Follow-up: What does the standalone example demonstrate? It installs both packages, declares a Query type, supplies a resolver, constructs ApolloServer, and starts the server.
GraphQL schemaresolversstartStandaloneServerExpressApollo ClientApollo RouterApollo Studioplugin API