1.5.8 • Published 5 years ago

@cat-org/koa-graphql v1.5.8

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

@cat-org/koa-graphql · npm npm-size

Collect the graphql files to build the graphql automatically.

Install

yarn add koa graphql @cat-org/koa-graphql

Use @cat-org/koa-graphql to server

  1. Add the middleware to server.
import Koa from 'koa';
import Graphql from '@cat-org/koa-graphql';

const app = new Koa();
const graphql = new Graphql('./path-to-graphql-foder');

app.use(graphql.middleware());
  1. Write the graphql files in ./path-to-graphql-foder.
// @flow

export default {
  typeDefs: `
  type Query {
    version: String!
  }
`,
  Query: {
    version: () => '1.0.0',
  },
};

Add the additional schema

Use with string:

const graphql = new Graphql('./path-to-graphql-foder', {
  typeDefs: `
  type AdditionalType {
    key: String!
  }
  `,
  resolvers: {
    AdditionalType: () => { ... },
  },
});

Use with Array:

const graphql = new Graphql('./path-to-graphql-foder', {
  typeDefs: [
    `
  type AdditionalType {
    key: String!
  }
`,
    `
  type AdditionalTwoType {
    key: String!
  }
`,
  ],
  resolvers: {
    AdditionalType: () => { ... },
    AdditionalTwoType: () => { ... },
  },
});

Update resolver with babel watch

const graphql = new Graphql('./path-to-graphql-folder');

chokidar
  .watch('./path-to-graphql-folder', {
    ignoreInitial: true,
  })
  .on('change', (filePath: string) => {
    graphql.update(filePath);
  });

Note: This function will only update the resolvers. This can not update the schema definition becuse graphql-tool does not support add new schema definition.

Give the options to makeExecutableSchema

Expect for typeDefs and resolvers, you can add the other options in makeExecutableSchema to this middleware.

const graphql = new Graphql('./path-to-graphql-foder', {
  options: { ... },
});

Give the options to express-graphql

Expect for schema, you can add the other options to this middleware.

...
app.use(graphql.middleware({ ... }));
...

Use to compile with relay-compiler

  1. Write a bin file or make server file to be a bin file.
#! /usr/bin/env node

import Koa from 'koa';
import Graphql from '@cat-org/koa-graphql';

const app = new Koa();
const graphql = new Graphql('./path-to-graphql-foder');

(async () => {
  if (process.env.RELAY_COMPILER) {
    await graphql.relay(['relay-compiler', 'command', 'without', 'schema']);
    process.exit();
  }

  app.use(graphql.middleware());
})();
  1. Run command with the bin file.
RELAY_COMPILER=true node ./server.js
1.5.8

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.5

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.0.0

5 years ago

1.0.0-beta.24

5 years ago