Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-yoga

Package Overview
Dependencies
Maintainers
1
Versions
3867
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-yoga

  • 5.10.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
449K
increased by12.52%
Maintainers
1
Weekly downloads
 
Created

What is graphql-yoga?

graphql-yoga is a fully-featured GraphQL server that is easy to set up and use. It is built on top of GraphQL.js and provides a simple yet powerful API for building GraphQL servers. It comes with out-of-the-box support for features like subscriptions, file uploads, and more.

What are graphql-yoga's main functionalities?

Basic Server Setup

This code sets up a basic GraphQL server with a single query 'hello' that returns a string. The server is started and listens on port 4000.

const { createServer } = require('graphql-yoga');
const typeDefs = `
  type Query {
    hello: String!
  }
`;
const resolvers = {
  Query: {
    hello: () => 'Hello, world!',
  },
};
const server = createServer({ typeDefs, resolvers });
server.start(() => console.log('Server is running on http://localhost:4000'));

Subscriptions

This code demonstrates how to set up a GraphQL server with subscriptions. It uses the 'graphql-subscriptions' package to handle real-time updates. A new message is published every second.

const { createServer } = require('graphql-yoga');
const { PubSub } = require('graphql-subscriptions');
const pubsub = new PubSub();
const typeDefs = `
  type Query {
    hello: String!
  }
  type Subscription {
    newMessage: String!
  }
`;
const resolvers = {
  Query: {
    hello: () => 'Hello, world!',
  },
  Subscription: {
    newMessage: {
      subscribe: () => pubsub.asyncIterator(['NEW_MESSAGE']),
    },
  },
};
const server = createServer({ typeDefs, resolvers });
server.start(() => console.log('Server is running on http://localhost:4000'));
setInterval(() => {
  pubsub.publish('NEW_MESSAGE', { newMessage: 'Hello, world!' });
}, 1000);

File Uploads

This code sets up a GraphQL server that supports file uploads. It defines a custom scalar 'Upload' and a mutation 'singleUpload' that handles the file upload process.

const { createServer } = require('graphql-yoga');
const typeDefs = `
  scalar Upload
  type Query {
    hello: String!
  }
  type Mutation {
    singleUpload(file: Upload!): String!
  }
`;
const resolvers = {
  Query: {
    hello: () => 'Hello, world!',
  },
  Mutation: {
    singleUpload: async (parent, { file }) => {
      const { createReadStream, filename } = await file;
      createReadStream().pipe(fs.createWriteStream(path.join(__dirname, filename)));
      return filename;
    },
  },
};
const server = createServer({ typeDefs, resolvers });
server.start(() => console.log('Server is running on http://localhost:4000'));

Other packages similar to graphql-yoga

Keywords

FAQs

Package last updated on 20 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc