使用 GraphQLSchema,是否有语法来模块化 GraphQLObjectTypes?
With GraphQLSchema, is there syntax to modularlize GraphQLObjectTypes?
如何将 userThat(可能还有其他几个相关字段)重构为其他不错的模块化文件,然后将它们包含到 root_query_type.js
这是香草 node/express/graphql GraphQLObjectType 实现。
还是我注定要有一个巨大的root_query_type.js?
schema.js
module.exports = new GraphQLSchema({
query: RootQueryType
});
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;
},
},
这是我通常做的事情:
// 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,
},
});
如何将 userThat(可能还有其他几个相关字段)重构为其他不错的模块化文件,然后将它们包含到 root_query_type.js
这是香草 node/express/graphql GraphQLObjectType 实现。
还是我注定要有一个巨大的root_query_type.js?
schema.js
module.exports = new GraphQLSchema({
query: RootQueryType
});
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;
},
},
这是我通常做的事情:
// 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,
},
});