28.0.12 • Published 5 months ago

@chillicream/nitro-express-middleware v28.0.12

Weekly downloads
-
License
SEE LICENSE FILE
Repository
github
Last release
5 months ago

Nitro Express Middleware

Express middleware for Nitro GraphQL IDE

Description

This middleware allows to setup Nitro GraphQL IDE for your GraphQL server.

You can use a cdn hosted version of the app or a self hosted version using the ad hoc package.

Installation

Install this package and the required peerDependencies in your project:

npm install @chillicream/nitro-express-middleware --save-dev
# or
yarn add @chillicream/nitro-express-middleware --dev
# or
pnpm add @chillicream/nitro-express-middleware --save-dev

Note: @chillicream/nitro-embedded is optional and only needed if you prefer to self host the app.

Usage

Add the middleware to your app.

import express from "express";
import nitroMiddleware from "@chillicream/nitro-express-middleware";

// ...

const app = express();

app.use(
  "/graphql",

  // for `cdn` hosted version
  nitroMiddleware({ mode: "cdn" })

  // for `embedded` version
  // nitroMiddleware({ mode: "embedded" }),

  // place here your graphql middleware and others
);

app.listen(3000, () => {
  console.log(`GraphQL on http://localhost:3000/graphql`);
});

Extended configuration

  • To pin a specific version instead of using "latest":

    nitroMiddleware({
      mode: "cdn",
      target: { version: "3.0.0" },
    });
  • To use your own infrastructure:

    nitroMiddleware({
      mode: "cdn",
      target: "https://mycompany.com/nitro",
    });

Custom options

  • To pass options supported by Nitro GraphQL IDE:

    nitroMiddleware({
      mode: "cdn",
      options: {
        title: "nitro",
      },
    });

Recipes

import express from "express";
import { createHandler } from "graphql-http";
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";

import nitroMiddleware from "@chillicream/nitro-express-middleware";

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: "Query",
    fields: {
      greeting: {
        type: GraphQLString,
        resolve(_parent, _args) {
          return "Hello, World!";
        },
      },
    },
  }),
});

const app = express();

const handler = createHandler({ schema });

app.use(
  "/graphql",

  // for `cdn` hosted version
  nitroMiddleware({ mode: "cdn" }),

  // for `embedded` version
  // nitroMiddleware({ mode: "embedded" }),

  async (req, res) => {
    try {
      const [body, init] = await handler({
        url: req.url,
        method: req.method,
        headers: req.headers,
        body: () =>
          new Promise((resolve) => {
            let body = "";
            req.on("data", (chunk) => (body += chunk));
            req.on("end", () => resolve(body));
          }),
        raw: req,
      });
      res.writeHead(init.status, init.statusText, init.headers).end(body);
    } catch (err) {
      res.writeHead(500).end(err.message);
    }
  }
);

app.listen(3000, () => {
  console.log(`GraphQL on http://localhost:3000/graphql`);
});
import express from "express";
import { createYoga, createSchema } from "graphql-yoga";

import nitroMiddleware from "@chillicream/nitro-express-middleware";

const graphQLServer = createYoga({
  schema: createSchema({
    typeDefs: /* GraphQL */ `
      type Query {
        greeting: String
      }
    `,
    resolvers: {
      Query: {
        greeting: () => "Hello, World!",
      },
    },
  }),
  logging: false,
});

const app = express();

app.use(
  "/graphql",

  // for `cdn` hosted version
  nitroMiddleware({ mode: "cdn" }),

  // for `embedded` version
  // nitroMiddleware({ mode: "embedded" }),

  graphQLServer
);

app.listen(3000, () => {
  console.log(`GraphQL on http://localhost:3000/graphql`);
});
import express from "express";
import { graphqlHTTP } from "express-graphql";
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";

import nitroMiddleware from "@chillicream/nitro-express-middleware";

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: "Query",
    fields: {
      greeting: {
        type: GraphQLString,
        resolve(_parent, _args) {
          return "Hello, World!";
        },
      },
    },
  }),
});

const app = express();

app.use(
  "/graphql",

  // for `cdn` hosted version
  nitroMiddleware({ mode: "cdn" }),

  // for `embedded` version
  // nitroMiddleware({ mode: "embedded" }),

  graphqlHTTP({
    schema,
    graphiql: false,
  })
);

app.listen(3000, () => {
  console.log(`GraphQL on http://localhost:3000/graphql`);
});
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import express from "express";
import http from "http";
import cors from "cors";
import bodyParser from "body-parser";

import nitroMiddleware from "@chillicream/nitro-express-middleware";

const typeDefs = `#graphql
  type Query {
    greeting: String
  }
`;

const resolvers = {
  Query: {
    greeting: () => "Hello, World!",
  },
};

const app = express();
const httpServer = http.createServer(app);
const server = new ApolloServer({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await server.start();

app.use(
  "/graphql",

  // for `cdn` hosted version
  nitroMiddleware({ mode: "cdn" }),

  // for `embedded` version
  // nitroMiddleware({ mode: "embedded" }),

  cors(),
  bodyParser.json(),
  expressMiddleware(server, {
    context: async ({ req }) => ({ token: req.headers.token }),
  })
);

httpServer.listen({ port: 3000 }, () => {
  console.log(`GraphQL on http://localhost:3000/graphql`);
});
26.0.0-insider.8

8 months ago

23.0.4

10 months ago

26.0.0-insider.7

8 months ago

23.0.3

10 months ago

26.0.0-insider.6

8 months ago

26.0.0-insider.5

8 months ago

26.0.0-insider.9

8 months ago

26.0.0-insider.4

8 months ago

23.0.0

11 months ago

26.0.0-insider.3

8 months ago

26.0.0-insider.2

9 months ago

23.0.2

10 months ago

26.0.0-insider.1

9 months ago

23.0.1

10 months ago

27.0.0-insider.1

8 months ago

27.0.0-insider.2

8 months ago

27.0.0-insider.3

8 months ago

27.0.0-insider.4

8 months ago

27.0.0-insider.5

8 months ago

27.0.0-insider.6

8 months ago

27.0.0-insider.7

8 months ago

28.0.0-insider.12

7 months ago

28.0.0-insider.13

7 months ago

28.0.0-insider.14

7 months ago

28.0.0-insider.10

7 months ago

28.0.0-insider.11

7 months ago

28.0.0-insider.1

8 months ago

28.0.0-insider.2

8 months ago

28.0.0-insider.7

7 months ago

28.0.0-insider.8

7 months ago

28.0.0-insider.9

7 months ago

28.0.0-insider.3

8 months ago

28.0.0-insider.4

8 months ago

28.0.0-insider.5

8 months ago

28.0.0-insider.6

8 months ago

27.0.7

7 months ago

27.0.0

8 months ago

27.0.2

8 months ago

27.0.1

8 months ago

27.0.4

8 months ago

27.0.3

8 months ago

27.0.6

8 months ago

27.0.5

8 months ago

25.0.0-insider.5

9 months ago

25.0.0-insider.4

9 months ago

25.0.0-insider.3

9 months ago

24.0.0-insider.10

10 months ago

25.0.0-insider.2

9 months ago

25.0.0-insider.1

9 months ago

25.0.1

9 months ago

25.0.0

9 months ago

25.0.0-insider.9

9 months ago

25.0.0-insider.8

9 months ago

25.0.0-insider.7

9 months ago

25.0.0-insider.6

9 months ago

22.0.0

1 year ago

29.0.0-insider.11

6 months ago

29.0.0-insider.12

6 months ago

29.0.0-insider.10

6 months ago

28.0.11

6 months ago

29.0.0-insider.15

6 months ago

24.0.0-insider.12

10 months ago

28.0.12

5 months ago

29.0.0-insider.16

6 months ago

29.0.0-insider.13

6 months ago

28.0.10

6 months ago

29.0.0-insider.14

6 months ago

29.0.0-insider.17

6 months ago

23.0.0-insider.5

11 months ago

23.0.0-insider.4

11 months ago

23.0.0-insider.7

11 months ago

23.0.0-insider.6

11 months ago

23.0.0-insider.3

11 months ago

23.0.0-insider.2

12 months ago

24.0.0-insider.1

11 months ago

26.0.0-insider.10

8 months ago

24.0.0-insider.5

10 months ago

24.0.0-insider.6

10 months ago

24.0.0-insider.3

11 months ago

24.0.0-insider.4

10 months ago

28.0.0

7 months ago

28.0.0-insider.23

7 months ago

24.0.0-insider.9

10 months ago

28.0.1

7 months ago

28.0.0-insider.24

7 months ago

24.0.0-insider.7

10 months ago

24.0.0-insider.8

10 months ago

28.0.4

7 months ago

28.0.5

6 months ago

28.0.2

7 months ago

28.0.0-insider.21

7 months ago

28.0.3

7 months ago

28.0.0-insider.22

7 months ago

28.0.8

6 months ago

29.0.0-insider.3

7 months ago

28.0.9

6 months ago

29.0.0-insider.2

7 months ago

28.0.6

6 months ago

29.0.0-insider.5

6 months ago

28.0.7

6 months ago

29.0.0-insider.4

7 months ago

26.0.4

8 months ago

29.0.0-insider.7

6 months ago

28.0.0-insider.16

7 months ago

26.0.3

8 months ago

25.0.0-insider.11

9 months ago

29.0.0-insider.6

6 months ago

28.0.0-insider.17

7 months ago

26.0.2

8 months ago

25.0.0-insider.12

9 months ago

29.0.0-insider.9

6 months ago

28.0.0-insider.18

7 months ago

26.0.1

8 months ago

29.0.0-insider.8

6 months ago

28.0.0-insider.19

7 months ago

26.0.0

8 months ago

25.0.0-insider.10

9 months ago

29.0.0-insider.1

7 months ago

25.0.0-insider.15

9 months ago

25.0.0-insider.13

9 months ago

25.0.0-insider.14

9 months ago

24.0.3

9 months ago

24.0.2

9 months ago

24.0.1

9 months ago

24.0.0

10 months ago

21.0.2

1 year ago

21.0.1

1 year ago

21.0.0

1 year ago

20.0.2

1 year ago

20.0.1

1 year ago

20.0.0

1 year ago

19.0.7

1 year ago

19.0.6

1 year ago

19.0.5

1 year ago

19.0.4

1 year ago

19.0.3

1 year ago

19.0.2

1 year ago

19.0.1

1 year ago

19.0.0

1 year ago