apollo-server vs graphql-compose vs graphql-tools vs type-graphql
GraphQL Server and Schema Design
apollo-servergraphql-composegraphql-toolstype-graphqlSimilar Packages:

GraphQL Server and Schema Design

GraphQL is a query language for APIs and a runtime for executing those queries by using a type system you define for your data. It allows clients to request only the data they need, making APIs more efficient and flexible. GraphQL servers are responsible for processing these queries and returning the requested data. They handle the execution of queries, mutations, and subscriptions based on the schema defined by the developer. These servers can be implemented in various programming languages and frameworks, providing a way to expose data and functionality to clients in a structured and efficient manner. apollo-server is a fully-featured GraphQL server implementation that is easy to set up and integrates well with various data sources. graphql-compose is a library that helps you build complex GraphQL schemas in a more modular and reusable way, especially when working with large schemas or multiple data sources. graphql-tools is a set of utilities for building and stitching GraphQL schemas, allowing you to create schemas from multiple sources and combine them into a single unified schema. type-graphql is a framework that leverages TypeScript decorators to build GraphQL schemas using classes and types, providing a more type-safe and intuitive way to define your schema.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
apollo-server013,93426.6 kB812 years agoMIT
graphql-compose01,210909 kB85a year agoMIT
graphql-tools05,4232.61 kB15910 days agoMIT
type-graphql08,087351 kB1142 months agoMIT

Feature Comparison: apollo-server vs graphql-compose vs graphql-tools vs type-graphql

Schema Definition

  • apollo-server:

    apollo-server allows you to define your schema using the Schema Definition Language (SDL) or programmatically. It provides a simple way to create a schema and resolvers, making it easy to set up a GraphQL server quickly.

  • graphql-compose:

    graphql-compose provides tools for defining schemas in a more modular way, allowing you to create reusable schema components and compose them together. This is especially useful for large schemas or when working with multiple data sources.

  • graphql-tools:

    graphql-tools focuses on schema stitching and federation, allowing you to combine schemas from multiple sources into a single unified schema. It provides utilities for creating schemas programmatically and merging them.

  • type-graphql:

    type-graphql uses TypeScript classes and decorators to define the schema, providing a more type-safe and intuitive way to create your schema. This approach leverages TypeScript's type system to ensure consistency between your schema and resolvers.

Integration with Data Sources

  • apollo-server:

    apollo-server integrates seamlessly with various data sources, including REST APIs, databases, and third-party services. It provides a flexible resolver system that allows you to fetch data from multiple sources and combine them in your GraphQL API.

  • graphql-compose:

    graphql-compose is designed to work with multiple data sources, allowing you to create schema components that can be reused across different parts of your application. It is particularly useful for integrating with databases, REST APIs, and other GraphQL schemas.

  • graphql-tools:

    graphql-tools supports schema stitching, which allows you to combine schemas from different data sources into a single schema. This is useful for creating a unified API from multiple services or microservices.

  • type-graphql:

    type-graphql integrates well with TypeScript-based data sources, such as ORMs (e.g., TypeORM, Prisma). It allows you to define your data models and resolvers in a type-safe manner, making it easier to work with databases and other data sources.

Code Example

  • apollo-server:

    apollo-server example

    const { ApolloServer, gql } = require('apollo-server');
    
    // Define schema
    const typeDefs = gql`
      type Query {
        hello: String
      }
    `;
    
    // Define resolvers
    const resolvers = {
      Query: {
        hello: () => 'Hello, world!',
      },
    };
    
    // Create server
    const server = new ApolloServer({ typeDefs, resolvers });
    
    // Start server
    server.listen().then(({ url }) => {
      console.log(`Server ready at ${url}`);
    });
    
  • graphql-compose:

    graphql-compose example

    const { schemaComposer } = require('graphql-compose');
    
    // Create a simple type
    const UserTC = schemaComposer.createObjectTC({
      name: 'User',
      fields: {
        id: 'ID!',
        name: 'String!',
      },
    });
    
    // Add a query
    schemaComposer.Query.addFields({
      user: {
        type: UserTC,
        resolve: () => ({ id: '1', name: 'John Doe' }),
      },
    });
    
    // Build schema
    const schema = schemaComposer.buildSchema();
    
  • graphql-tools:

    graphql-tools example

    const { makeExecutableSchema } = require('@graphql-tools/schema');
    
    // Define type definitions
    const typeDefs = `
      type Query {
        hello: String
      }
    `;
    
    // Define resolvers
    const resolvers = {
      Query: {
        hello: () => 'Hello, world!',
      },
    };
    
    // Create executable schema
    const schema = makeExecutableSchema({ typeDefs, resolvers });
    
  • type-graphql:

    type-graphql example

    import 'reflect-metadata';
    import { ObjectType, Field, ID, Resolver, Query, buildSchema } from 'type-graphql';
    import { ApolloServer } from 'apollo-server';
    
    @ObjectType()
    class User {
      @Field(type => ID)
      id: string;
    
      @Field()
      name: string;
    }
    
    @Resolver()
    class UserResolver {
      @Query(returns => User)
      user() {
        return { id: '1', name: 'John Doe' };
      }
    }
    
    async function main() {
      const schema = await buildSchema({
        resolvers: [UserResolver],
      });
    
      const server = new ApolloServer({ schema });
      server.listen(4000, () => {
        console.log('Server is running on http://localhost:4000');
      });
    }
    
    main();
    

How to Choose: apollo-server vs graphql-compose vs graphql-tools vs type-graphql

  • apollo-server:

    Choose apollo-server if you need a quick and easy way to set up a GraphQL server with minimal configuration. It is ideal for projects where you want a fully-featured server out of the box, with support for subscriptions, middleware, and integrations with various data sources.

  • graphql-compose:

    Choose graphql-compose if you are working on a large or complex schema that requires modularity and reusability. It is particularly useful for projects where you need to compose schemas from multiple sources or want to create reusable schema components.

  • graphql-tools:

    Choose graphql-tools if you need to stitch together schemas from multiple sources or create a schema programmatically. It is ideal for projects that require schema federation or need to combine schemas from different services.

  • type-graphql:

    Choose type-graphql if you are using TypeScript and want to leverage its type system to create your GraphQL schema. It is ideal for projects that prefer a code-first approach and want to benefit from type safety and decorators.

README for apollo-server

Apollo Server

A TypeScript GraphQL Server for Express, Koa, Hapi, Lambda, and more.

npm version Build Status Join the community forum Read CHANGELOG

Apollo Server is a community-maintained open-source GraphQL server. It works with many Node.js HTTP server frameworks, or can run on its own with a built-in Express server. Apollo Server works with any GraphQL schema built with GraphQL.js--or define a schema's type definitions using schema definition language (SDL).

Read the documentation for information on getting started and many other use cases and follow the CHANGELOG for updates.

Principles

Apollo Server is built with the following principles in mind:

  • By the community, for the community: Its development is driven by the needs of developers.
  • Simplicity: By keeping things simple, it is more secure and easier to implement and contribute.
  • Performance: It is well-tested and production-ready.

Anyone is welcome to contribute to Apollo Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!

Getting started

To get started with Apollo Server:

  • Install with npm install apollo-server-<integration> graphql
  • Write a GraphQL schema
  • Use one of the following snippets

There are two ways to install Apollo Server:

  • Standalone: For applications that do not require an existing web framework, use the apollo-server package.
  • Integrations: For applications with a web framework (e.g. express, koa, hapi, etc.), use the appropriate Apollo Server integration package.

For more info, please refer to the Apollo Server docs.

Installation: Standalone

In a new project, install the apollo-server and graphql dependencies using:

npm install apollo-server graphql

Then, create an index.js which defines the schema and its functionality (i.e. resolvers):

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

// The GraphQL schema
const typeDefs = gql`
  type Query {
    "A simple type for getting started!"
    hello: String
  }
`;

// A map of functions which return data for the schema.
const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

server.listen().then(({ url }) => {
  console.log(`πŸš€ Server ready at ${url}`);
});

Due to its human-readability, we recommend using schema-definition language (SDL) to define a GraphQL schema--a GraphQLSchema object from graphql-js can also be specified instead of typeDefs and resolvers using the schema property:

const server = new ApolloServer({
  schema: ...
});

Finally, start the server using node index.js and go to the URL returned on the console.

For more details, check out the Apollo Server Getting Started guide and the fullstack tutorial.

For questions, the Apollo community forum is a great place to get help.

Installation: Integrations

While the standalone installation above can be used without making a decision about which web framework to use, the Apollo Server integration packages are paired with specific web frameworks (e.g. Express, Koa, hapi).

The following web frameworks have Apollo Server integrations, and each of these linked integrations has its own installation instructions and examples on its package README.md:

Context

A request context is available for each request. When context is defined as a function, it will be called on each request and will receive an object containing a req property, which represents the request itself.

By returning an object from the context function, it will be available as the third positional parameter of the resolvers:

new ApolloServer({
  typeDefs,
  resolvers: {
    Query: {
      books: (parent, args, context, info) => {
        console.log(context.myProperty); // Will be `true`!
        return books;
      },
    }
  },
  context: async ({ req }) => {
    return {
      myProperty: true
    };
  },
})

Documentation

The Apollo Server documentation contains additional details on how to get started with GraphQL and Apollo Server.

The raw Markdown source of the documentation is available within the docs/ directory of this monorepo--to contribute, please use the Edit on GitHub buttons at the bottom of each page.

Development

If you wish to develop or contribute to Apollo Server, we suggest the following:

  • Fork this repository

  • Install Direnv (a tool that automatically sets up environment variables in project directories) or nvm. We use nvm to ensure we're running the expected version of Node (and we use Direnv to install and run nvm automatically).

  • Install the Apollo Server project on your computer

git clone https://github.com/[your-user]/apollo-server
cd apollo-server
direnv allow  # sets up nvm for you; if you installed nvm yourself, try `nvm install` instead
  • Build and test
npm install
npm test
  • To run individual test files, run npm run pretest && npx jest packages/apollo-server-foo/src/__tests__/bar.test.ts. Note that you do need to re-compile TypeScript before each time you run a test, or changes across packages may not be picked up. Instead of running npm run pretest from scratch before each test run, you can also run tsc --build tsconfig.json --watch in another shell, or use the VSCode Run Build Task to run that for you.

Community

Are you stuck? Want to contribute? Come visit us in the Apollo community forum!

Maintainers

Who is Apollo?

Apollo builds open-source software and a graph platform to unify GraphQL across your apps and services. We help you ship faster with:

  • Apollo Studio – A free, end-to-end platform for managing your GraphQL lifecycle. Track your GraphQL schemas in a hosted registry to create a source of truth for everything in your graph. Studio provides an IDE (Apollo Explorer) so you can explore data, collaborate on queries, observe usage, and safely make schema changes.
  • Apollo Federation – The industry-standard open architecture for building a distributed graph. Use Apollo’s gateway to compose a unified graph from multiple subgraphs, determine a query plan, and route requests across your services.
  • Apollo Client – The most popular GraphQL client for the web. Apollo also builds and maintains Apollo iOS and Apollo Android.
  • Apollo Server – A production-ready JavaScript GraphQL server that connects to any microservice, API, or database. Compatible with all popular JavaScript frameworks and deployable in serverless environments.

Learn how to build with Apollo

Check out the Odyssey learning platform, the perfect place to start your GraphQL journey with videos and interactive code challenges. Join the Apollo Community to interact with and get technical help from the GraphQL community.