Socket
Socket
Sign inDemoInstall

express-graphql

Package Overview
Dependencies
Maintainers
7
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-graphql

Production ready GraphQL HTTP middleware.


Version published
Maintainers
7
Created

What is express-graphql?

The express-graphql package is a middleware for integrating GraphQL with an Express server. It allows you to create a GraphQL HTTP server with Express, enabling you to define a schema, resolve functions, and handle GraphQL queries and mutations.

What are express-graphql's main functionalities?

Setting up a basic GraphQL server

This code sets up a basic GraphQL server using express-graphql. It defines a simple schema with a single query 'hello' and a root resolver that returns 'Hello world!'. The server listens on port 4000 and provides a GraphiQL interface for testing queries.

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const root = { hello: () => 'Hello world!' };

const app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));

app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));

Enabling GraphiQL interface

This code demonstrates how to enable the GraphiQL interface, an in-browser IDE for exploring GraphQL. By setting the 'graphiql' option to true, you can navigate to '/graphql' in your browser and interact with your GraphQL API.

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const root = { hello: () => 'Hello world!' };

const app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));

app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));

Handling mutations

This code shows how to handle mutations in a GraphQL server. It defines a schema with a 'setMessage' mutation and a 'getMessage' query. The 'setMessage' mutation updates a message variable, and the 'getMessage' query retrieves the current message.

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

const schema = buildSchema(`
  type Mutation {
    setMessage(message: String): String
  }
  type Query {
    getMessage: String
  }
`);

let message = 'Hello world!';

const root = {
  setMessage: ({ message: newMessage }) => {
    message = newMessage;
    return message;
  },
  getMessage: () => message,
};

const app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));

app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));

Other packages similar to express-graphql

Keywords

FAQs

Package last updated on 05 Jul 2020

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