Convert Figma logo to code with AI

graphql-hive logographql-yoga

🧘 Rewrite of a fully-featured GraphQL Server with focus on easy setup, performance & great developer experience. The core of Yoga implements WHATWG Fetch API and can run/deploy on any JS environment.

8,359
575
8,359
159

Top Related Projects

🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.

A reference implementation of GraphQL for JavaScript

5,982

Simple GraphQL Client for JavaScript. Minimal. Extensible. Type Safe. Runs everywhere.

3,410

Code-First, Type-Safe, GraphQL Schema Construction

Implement GraphQL servers and gateways with Fastify

Quick Overview

GraphQL Yoga is a fully-featured GraphQL server with focus on easy setup, performance and great developer experience. It's built on top of popular libraries like GraphQL.js and Express, and works with any GraphQL schema.

Pros

  • Easy to set up and use, with sensible defaults
  • Highly customizable and extensible
  • Built-in support for subscriptions and file uploads
  • Compatible with various environments (Node.js, Deno, Cloudflare Workers, etc.)

Cons

  • May have a steeper learning curve for those new to GraphQL
  • Some advanced features might require additional configuration
  • Documentation could be more comprehensive for complex use cases

Code Examples

  1. Basic server setup:
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'

const yoga = createYoga({
  schema: {
    typeDefs: `
      type Query {
        hello: String
      }
    `,
    resolvers: {
      Query: {
        hello: () => 'Hello from Yoga!'
      }
    }
  }
})

const server = createServer(yoga)
server.listen(4000, () => {
  console.log('Server is running on http://localhost:4000/graphql')
})
  1. Using with Express:
import express from 'express'
import { createYoga } from 'graphql-yoga'

const app = express()

const yoga = createYoga({
  schema: {
    typeDefs: `
      type Query {
        hello: String
      }
    `,
    resolvers: {
      Query: {
        hello: () => 'Hello from Yoga with Express!'
      }
    }
  }
})

app.use('/graphql', yoga)
app.listen(4000, () => {
  console.log('Server is running on http://localhost:4000/graphql')
})
  1. Adding subscriptions:
import { createServer } from 'node:http'
import { createYoga, createPubSub } from 'graphql-yoga'

const pubSub = createPubSub()

const yoga = createYoga({
  schema: {
    typeDefs: `
      type Query {
        hello: String
      }
      type Subscription {
        greetings: String
      }
    `,
    resolvers: {
      Query: {
        hello: () => 'Hello'
      },
      Subscription: {
        greetings: {
          subscribe: () => pubSub.subscribe('GREETINGS'),
          resolve: (payload) => payload
        }
      }
    }
  }
})

const server = createServer(yoga)
server.listen(4000, () => {
  console.log('Server is running on http://localhost:4000/graphql')
  setInterval(() => {
    pubSub.publish('GREETINGS', 'Hello from subscription!')
  }, 1000)
})

Getting Started

To get started with GraphQL Yoga:

  1. Install the package:

    npm install graphql-yoga
    
  2. Create a new file (e.g., server.js) with the basic server setup from the first code example above.

  3. Run the server:

    node server.js
    
  4. Open your browser and navigate to http://localhost:4000/graphql to access the GraphQL Playground and start querying your server.

Competitor Comparisons

🌍  Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.

Pros of Apollo Server

  • More extensive documentation and community support
  • Robust integration with Apollo ecosystem tools
  • Advanced features like caching and subscription support

Cons of Apollo Server

  • Heavier and more complex setup for simple use cases
  • Less flexible in terms of HTTP server integration
  • Steeper learning curve for beginners

Code Comparison

Apollo Server:

const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');

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

GraphQL Yoga:

import { createServer } from '@graphql-yoga/node'
import { schema } from './schema'

const server = createServer({ schema })
server.start()

GraphQL Yoga offers a more lightweight and flexible approach, making it easier to integrate with existing HTTP servers and frameworks. It provides a simpler setup process and is more suitable for smaller projects or those requiring custom configurations.

Apollo Server, on the other hand, offers a more comprehensive solution with advanced features and tight integration with the Apollo ecosystem. It's well-suited for larger, more complex applications that can benefit from its extensive tooling and scalability options.

Both libraries are actively maintained and have their strengths, so the choice between them often depends on the specific requirements of the project and the developer's familiarity with each ecosystem.

A reference implementation of GraphQL for JavaScript

Pros of graphql-js

  • Reference implementation of GraphQL specification
  • Comprehensive and well-tested codebase
  • Widely adopted and supported by the GraphQL community

Cons of graphql-js

  • Lower-level implementation, requiring more setup and configuration
  • Less opinionated, which can lead to more boilerplate code
  • Fewer built-in features for modern GraphQL development

Code Comparison

GraphQL.js:

const { GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql');

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'Hello, World!';
        }
      }
    }
  })
});

GraphQL Yoga:

import { createSchema, createYoga } from 'graphql-yoga';

const schema = createSchema({
  typeDefs: `
    type Query {
      hello: String
    }
  `,
  resolvers: {
    Query: {
      hello: () => 'Hello, World!'
    }
  }
});

const yoga = createYoga({ schema });

GraphQL Yoga provides a more streamlined and modern approach to setting up a GraphQL server, with built-in features like automatic schema generation and easier integration with various plugins. GraphQL.js, on the other hand, offers more flexibility and control over the implementation details, making it suitable for complex or custom GraphQL setups.

5,982

Simple GraphQL Client for JavaScript. Minimal. Extensible. Type Safe. Runs everywhere.

Pros of Graffle

  • Simpler setup and configuration for basic GraphQL servers
  • Lightweight and focused specifically on GraphQL functionality
  • Easier learning curve for developers new to GraphQL

Cons of Graffle

  • Less comprehensive feature set compared to GraphQL Yoga
  • Smaller community and ecosystem support
  • May require additional libraries for advanced use cases

Code Comparison

GraphQL Yoga:

import { createServer } from '@graphql-yoga/node'

const server = createServer({
  schema: {
    typeDefs: /* GraphQL */ `
      type Query {
        hello: String
      }
    `,
    resolvers: {
      Query: {
        hello: () => 'Hello from Yoga!'
      }
    }
  }
})

server.start()

Graffle:

import { Graffle } from 'graffle'

const graffle = new Graffle({
  typeDefs: `
    type Query {
      hello: String
    }
  `,
  resolvers: {
    Query: {
      hello: () => 'Hello from Graffle!'
    }
  }
})

graffle.start()

Both GraphQL Yoga and Graffle provide simple ways to set up a GraphQL server, but GraphQL Yoga offers more advanced features and integrations out of the box. Graffle focuses on simplicity and ease of use, making it a good choice for smaller projects or those new to GraphQL. However, for larger or more complex applications, GraphQL Yoga's extensive feature set and community support may be more beneficial.

3,410

Code-First, Type-Safe, GraphQL Schema Construction

Pros of Nexus

  • Strong TypeScript support with auto-generated types
  • Declarative schema definition with a code-first approach
  • Extensive plugin ecosystem for enhanced functionality

Cons of Nexus

  • Steeper learning curve for developers new to code-first GraphQL
  • Less flexibility in schema customization compared to SDL-first approaches
  • Potentially larger bundle size due to its comprehensive feature set

Code Comparison

Nexus:

import { objectType, queryType, makeSchema } from 'nexus'

const User = objectType({
  name: 'User',
  definition(t) {
    t.string('id')
    t.string('name')
  },
})

const Query = queryType({
  definition(t) {
    t.list.field('users', {
      type: 'User',
      resolve: () => [{ id: '1', name: 'Alice' }],
    })
  },
})

const schema = makeSchema({ types: [User, Query] })

GraphQL Yoga:

import { createSchema, createYoga } from 'graphql-yoga'

const schema = createSchema({
  typeDefs: `
    type User {
      id: ID!
      name: String!
    }
    type Query {
      users: [User!]!
    }
  `,
  resolvers: {
    Query: {
      users: () => [{ id: '1', name: 'Alice' }],
    },
  },
})

const yoga = createYoga({ schema })

Implement GraphQL servers and gateways with Fastify

Pros of Mercurius

  • Tightly integrated with Fastify, offering excellent performance and a familiar ecosystem for Fastify users
  • Built-in support for subscriptions and file uploads without additional plugins
  • Automatic persisted queries feature for improved network performance

Cons of Mercurius

  • Limited to the Fastify framework, less flexible for other Node.js environments
  • Smaller community and ecosystem compared to GraphQL Yoga
  • Less extensive documentation and fewer examples available

Code Comparison

Mercurius:

const fastify = require('fastify')()
fastify.register(require('mercurius'), {
  schema,
  resolvers
})
fastify.listen(3000)

GraphQL Yoga:

import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'

const yoga = createYoga({ schema })
const server = createServer(yoga)
server.listen(3000)

Both Mercurius and GraphQL Yoga are powerful GraphQL server implementations, but they cater to different use cases. Mercurius excels in Fastify-based applications, offering tight integration and performance benefits. GraphQL Yoga, on the other hand, provides more flexibility and a larger ecosystem, making it suitable for various Node.js environments. The choice between the two depends on the specific project requirements and the preferred framework.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

GraphQL Yoga

Fully-featured GraphQL Server with focus on easy setup, performance & great developer experience
Go to documentation

npm bundlephobia minified size bundlephobia minified+zipped size bundlephobia treeshaking license

Quick start

Install

pnpm add graphql-yoga graphql

Start

Make a schema, create Yoga and start a Node server:

import { createServer } from 'node:http'
import { createSchema, createYoga } from 'graphql-yoga'

const yoga = createYoga({
  schema: createSchema({
    typeDefs: /* GraphQL */ `
      type Query {
        hello: String
      }
    `,
    resolvers: {
      Query: {
        hello: () => 'Hello from Yoga!'
      }
    }
  })
})

const server = createServer(yoga)

server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})

Overview

  • Easiest way to run a GraphQL server: Sensible defaults & includes everything you need with minimal setup (we also export a platform/env-agnostic handler so you can build your own wrappers easily).
  • Includes Subscriptions: Built-in support for GraphQL subscriptions using Server-Sent Events.
  • Compatible: Works with all GraphQL clients (Apollo, Relay, Urql...) and fits seamless in your GraphQL workflow.
  • WHATWG Fetch API: the core package depends on WHATWG Fetch API so it can run and deploy on any environment (Serverless, Workers, Deno, Node).
  • Easily Extendable: New GraphQL-Yoga support all envelop plugins.

Features

Documentation

Our documentation website will help you get started.

Examples

We've made sure developers can quickly start with GraphQL Yoga by providing a comprehensive set of examples. See all of them in the examples/ folder.

Comparison

Read more about how GraphQL Yoga compares to other servers in the ecosystem here.

Contributing

If this is your first time contributing to this project, please do read our Contributor Workflow Guide before you get started off.

For this project in particular, to get started on stage/2-failing-test:

  1. Install Node.js
  2. Run in your terminal: npm i -g pnpm@8 && pnpm install && pnpm build
  3. Add tests to packages/graphql-yoga/__tests__ using Jest APIs
  4. Run the tests with pnpm test

Feel free to open issues and pull requests. We're always welcome support from the community.

Code of Conduct

Help us keep Yoga open and inclusive. Please read and follow our Code of Conduct as adopted from Contributor Covenant.

License

MIT

NPM DownloadsLast 30 Days