18.0.0 • Published 9 months ago

@chillicream/bananacakepop-express-middleware v18.0.0

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

Banana Cake Pop Express Middleware

Express middleware for Banana Cake Pop GraphQL IDE

Description

This middleware allows to setup Banana Cake Pop 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/bananacakepop-express-middleware --save-dev
# or
yarn add @chillicream/bananacakepop-express-middleware --dev
# or
pnpm add @chillicream/bananacakepop-express-middleware --save-dev

Note: @chillicream/bananacakepop-graphql-ide 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 bcpMiddleware from "@chillicream/bananacakepop-express-middleware";

// ...

const app = express();

app.use(
  "/graphql",

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

  // for `self` hosted version
  // bcpMiddleware({ mode: "self" }),

  // 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":

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

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

Custom options

  • To pass options supported by Banana Cake Pop GraphQL IDE:

    bcpMiddleware({
      mode: "cdn",
      options: {
        title: "BCP",
      },
    });

Recipes

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

import bcpMiddleware from "@chillicream/bananacakepop-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
  bcpMiddleware({ mode: "cdn" }),

  // for `self` hosted version
  // bcpMiddleware({ mode: "self" }),

  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 bcpMiddleware from "@chillicream/bananacakepop-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
  bcpMiddleware({ mode: "cdn" }),

  // for `self` hosted version
  // bcpMiddleware({ mode: "self" }),

  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 bcpMiddleware from "@chillicream/bananacakepop-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
  bcpMiddleware({ mode: "cdn" }),

  // for `self` hosted version
  // bcpMiddleware({ mode: "self" }),

  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 bcpMiddleware from "@chillicream/bananacakepop-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
  bcpMiddleware({ mode: "cdn" }),

  // for `self` hosted version
  // bcpMiddleware({ mode: "self" }),

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

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

9 months ago

18.0.0-insider.2

9 months ago

18.0.0

9 months ago

17.0.1-insider.2

10 months ago

17.0.1-insider.4

9 months ago

17.0.1-insider.1

10 months ago

17.0.0

11 months ago

17.0.0-insider.1

12 months ago

17.0.0-insider.2

11 months ago

17.0.0-insider.3

11 months ago

17.0.0-insider.4

11 months ago

16.0.2

1 year ago

16.0.3

1 year ago

16.0.1

1 year ago

16.0.0

1 year ago

15.0.4

1 year ago

15.0.3

1 year ago

15.0.2

1 year ago

15.0.1

1 year ago

15.0.0

1 year ago

15.0.0-insider.2

2 years ago

15.0.0-insider.1

2 years ago

14.0.0

2 years ago

14.0.0-insider.6

2 years ago

14.0.0-insider.7

2 years ago

14.0.0-insider.5

2 years ago

14.0.0-insider.4

2 years ago

14.0.0-insider.3

2 years ago

14.0.0-insider.2

2 years ago

14.0.0-insider.1

2 years ago

13.0.0-insider.3

2 years ago

13.0.0-insider.2

2 years ago

13.0.0

2 years ago

13.0.0-insider.1

2 years ago

12.0.5

2 years ago

11.0.0-insider.7

2 years ago

11.0.0-insider.8

2 years ago

12.0.3

2 years ago

12.0.4

2 years ago

12.0.1

2 years ago

12.0.2

2 years ago

11.0.0

2 years ago

12.0.0-insider.1

2 years ago

10.0.5

2 years ago

10.0.6

2 years ago

10.0.7

2 years ago

11.0.0-insider.6

2 years ago

11.0.0-insider.3

2 years ago

11.0.0-insider.1

2 years ago

9.0.4

2 years ago

11.0.0-insider.2

2 years ago

9.0.3

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

10.0.2

2 years ago

10.0.4

2 years ago

10.0.0-insider.3

2 years ago

10.0.0-insider.2

2 years ago

10.0.0-insider.5

2 years ago

10.0.0-insider.4

2 years ago

10.0.0-insider.6

2 years ago

10.0.0-insider.9

2 years ago

8.0.0-insider.1

2 years ago

8.0.0-insider.2

2 years ago

7.0.0-insider.6

2 years ago

7.0.0-insider.5

2 years ago

10.0.0-insider.1

2 years ago

7.0.0

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

9.0.2

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

9.0.0-insider.6

2 years ago

9.0.0-insider.7

2 years ago

9.0.0-insider.4

2 years ago

9.0.0-insider.5

2 years ago

9.0.0-insider.2

2 years ago

9.0.0-insider.3

2 years ago

9.0.0-insider.1

2 years ago

9.0.0-insider.8

2 years ago

9.0.0-insider.10

2 years ago

9.0.0-insider.9

2 years ago

9.0.0-insider.11

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.2

2 years ago

7.0.0-insider.4

2 years ago

6.0.0-insider.3

2 years ago

6.0.0-insider.4

2 years ago

6.0.0-insider.5

2 years ago

6.0.0-insider.6

2 years ago

6.0.0-insider.1

2 years ago

6.0.0-insider.2

2 years ago

7.0.0-insider.3

2 years ago

7.0.0-insider.2

2 years ago

7.0.0-insider.1

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.2

2 years ago

5.0.8

2 years ago

5.0.7

2 years ago

5.0.6

2 years ago

5.0.5

2 years ago

5.0.4

2 years ago

5.0.3

2 years ago

5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

5.0.0-insider.34

2 years ago

5.0.0-insider.33

2 years ago

5.0.0-insider.32

2 years ago

5.0.0-insider.31

2 years ago

5.0.0-insider.30

2 years ago

5.0.0-insider.29

2 years ago

5.0.0-insider.28

2 years ago

5.0.0-insider.27

2 years ago

5.0.0-insider.19

2 years ago

5.0.0-insider.18

2 years ago

5.0.0-insider.11

2 years ago

5.0.0-insider.10

2 years ago

5.0.0-insider.26

2 years ago

5.0.0-insider.25

2 years ago

5.0.0-insider.8

2 years ago

5.0.0-insider.24

2 years ago

5.0.0-insider.9

2 years ago

5.0.0-insider.23

2 years ago

5.0.0-insider.22

2 years ago

5.0.0-insider.21

2 years ago

5.0.0-insider.20

2 years ago

5.0.0-insider.6

2 years ago

5.0.0-insider.7

2 years ago

4.0.1

2 years ago

5.0.0-insider.3

2 years ago

5.0.0-insider.4

2 years ago

5.0.0-insider.5

2 years ago

5.0.0-insider.1

2 years ago

5.0.0-insider.2

2 years ago

4.0.0

2 years ago

4.0.0-insider.8

2 years ago

4.0.0-insider.7

2 years ago

4.0.0-insider.6

2 years ago

4.0.0-insider.5

2 years ago

4.0.0-insider.4

2 years ago

4.0.0-insider.3

2 years ago

3.0.5

2 years ago

4.0.0-insider.2

3 years ago

3.0.4

3 years ago

4.0.0-insider.1

3 years ago

3.0.3

3 years ago

3.0.0-insider.23

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-insider.22

3 years ago

3.0.0-insider.21

3 years ago

3.0.0-insider.20

3 years ago