Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 构建一个Express+;GraphQL项目_Javascript_Node.js_Express_Structure_Graphql - Fatal编程技术网

Javascript 构建一个Express+;GraphQL项目

Javascript 构建一个Express+;GraphQL项目,javascript,node.js,express,structure,graphql,Javascript,Node.js,Express,Structure,Graphql,如何保持GraphQLAPI整洁 我有三种可查询类型的简单端点,并保持整洁的文件夹结构,如下所示: /api |--/entry |--/types.js |--/queries.js |--/mutations.js |--/category |--/types.js |--/queries.js |--/mutations.js |--/service |--/types.js |--

如何保持GraphQLAPI整洁

我有三种可查询类型的简单端点,并保持整洁的文件夹结构,如下所示:

/api
  |--/entry
      |--/types.js
      |--/queries.js
      |--/mutations.js
  |--/category
      |--/types.js
      |--/queries.js
      |--/mutations.js
  |--/service
      |--/types.js
      |--/queries.js
      |--/mutations.js
  |--index.js
js是我定义根变异和查询的地方,它要求我显式地引用我的类型、查询和变异,所以很明显,随着我添加新类型,这个文件会增长,这有点挫败了首先将它们拆分到不同文件夹中的观点

// src/routes/api/index.js

const LOGGER = require('../../logger')('routes/api');
const EXPRESS_GRAPHQL = require('express-graphql');

// returned when requiring this module
const routes = function (handler) {

    LOGGER.log('info', 'Setting up api routes.');

    const {
        GraphQLSchema,
        GraphQLObjectType,
    } = require('graphql');

    const categoryQuery = require('./category/queries');
    const {
        updateCategory,
        deleteCategory,
        createCategory
    } = require('./category/mutations');

    const serviceQuery = require('./service/queries');
    const {
        updateService,
        deleteService,
        createService
    } = require('./service/mutations');

    const entryQuery = require('./entry/queries');
    const {
        updateEntry,
        deleteEntry,
        createEntry
    } = require('./entry/mutations');

    const RootQuery = new GraphQLObjectType({
        name: 'rootQuery',
        description: 'This is the root query which holds all possible READ entrypoints for the GraphQL API',
        fields: () => ({
            service: serviceQuery,
            category: categoryQuery,
            entry: entryQuery
        })
    });

    const RootMutation = new GraphQLObjectType({
        name: 'rootMutation',
        description: 'This is the root mutation which holds all possible WRITE entrypoints for the GraphQL API',
        fields: () => ({
            updateCategory,
            deleteCategory,
            createCategory,
            updateEntry,
            deleteEntry,
            createEntry,
            updateService,
            deleteService,
            createService
        })
    });

    const Schema = new GraphQLSchema({
        query: RootQuery,
        mutation: RootMutation
    });

    return EXPRESS_GRAPHQL({
        schema: Schema,
        pretty: true,
        graphiql: true,
    });
};

module.exports = routes;

我怎样才能避免这种情况呢?

好吧,这一点也没有违背目的——应用程序中的关注点已经很好地分开了,而
index.js
增长的唯一原因是需要明确地要求所有模块

我们努力解决了类似的问题,最终为每个模型目录(
条目/
类别/
服务/
)应该包含什么以及内部模块应该导出什么设置了严格的接口。有鉴于此,您可以编写一个整洁的包装器函数,在启动时动态列出所有模型目录,并尝试加载它们,期望使用所述接口。正因为如此,您根本不必在
index.js
中编写任何新代码。您只需复制粘贴一个目录,实现导出的函数,它们就会在应用程序启动时自动加载

请记住在加载程序中添加良好的错误日志记录,以避免意外破坏接口契约时出现隐藏的错误