@mikojs/koa-graphql ·
Collect the graphql files to build the graphql automatically.
Install
yarn add koa graphql @mikojs/koa-graphql
@mikojs/koa-graphql
to server
Use - Add the middleware to server.
import Koa from 'koa';
import Graphql from '@mikojs/koa-graphql';
const app = new Koa();
const graphql = new Graphql('./path-to-graphql-foder');
app.use(graphql.middleware());
- 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: () => { ... },
},
});
babel watch
Update resolver with 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.
makeExecutableSchema
Give the options toExpect for typeDefs
and resolvers
, you can add the other options in makeExecutableSchema
to this middleware.
const graphql = new Graphql('./path-to-graphql-foder', {
options: { ... },
});
express-graphql
Give the options toExpect for schema
, you can add the other options to this middleware.
...
app.use(graphql.middleware({ ... }));
...
Use to compile with relay-compiler
- Write a bin file or make server file to be a bin file.
#! /usr/bin/env node
import Koa from 'koa';
import Graphql from '@mikojs/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());
})();
- Run command with the bin file.
RELAY_COMPILER=true node ./server.js
Use in the unit testing
import Graphql from '@mikojs/koa-graphql';
const graphql = new Graphql('./path-to-graphql-foder');
test('test', async () => {
expect(await graphql.query({
source: '{ version }',
...
})).toEqual({ ... });
});
This function use graphql
to query, you can see here to lerna the more information.