Socket
Socket
Sign inDemoInstall

graphql-yoga

Package Overview
Dependencies
Maintainers
0
Versions
3752
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-yoga

[![GraphQLConf 2024 Banner: September 10-12, San Francisco. Hosted by the GraphQL Foundation](https://github.com/user-attachments/assets/bdb8cd5d-5186-4ece-b06b-b00a499b7868)](https://graphql.org/conf/2024/?utm_source=github&utm_medium=graphql_yoga&utm_ca


Version published
Weekly downloads
396K
increased by2.63%
Maintainers
0
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 15 Aug 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