简体   繁体   English

使用 GraphQLSchema,是否有模块化 GraphQLObjectTypes 的语法?

[英]With GraphQLSchema, is there syntax to modularlize GraphQLObjectTypes?

How do I refactor userThat (and likely several other related fields) into other nice modular files and then include them into root_query_type.js我如何将 userThat(可能还有其他几个相关字段)重构为其他不错的模块化文件,然后将它们包含到 root_query_type.js 中

This is a vanilla node/express/graphql GraphQLObjectType implementation.这是一个普通的 node/express/graphql GraphQLObjectType 实现。

Or am I destined to have a huge root_query_type.js ?还是我注定要拥有一个巨大的 root_query_type.js ?

schema.js架构.js

module.exports = new GraphQLSchema({
  query: RootQueryType
});

root_query_type.js root_query_type.js

const RootQueryType = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    userThis: {
      type: UserType,
      resolve(parentValue, args, request) {
        return thisResult;
      }
    },
    userThat: {
      type: UserType,
      args: {
        thatArgs: { type: new GraphQLNonNull(GraphQLString) },
      },
      resolve: (parentValue, arg, request) => {
        return thatResult;
      },
    },

Here is what I usually do:这是我通常做的:

// user.js

const User = new GraphQLObjectType({ ... });

const queryFields = {
  userThis: {
    type: UserType,
    resolve(parentValue, args, request) {
      return thisResult;
    }
  },
};

const mutationFields = { ... };

module.exports = { User, queryFields, mutationFields };
// RootQueryType.js

// rename queryFields to userQueryFields as we might want to import queryFields
// from many different modules
const { queryFields: userQueryFields } = require('./modules/user.js');

const RootQueryType = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    ...userQueryFields,
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM