无法在架构变更中从另一个schema.graphql文件中的另一个schema.graphql文件访问共享架构

时间:2019-04-16 14:41:11

标签: typescript graphql apollo-server graphql-tools

我在userSchema.graphql文件中有用户架构;

  id: String!
  userName: String!
  email: String!
  password: String!
}

我有不同的schema.graphql文件用于登录和注册

并且我无法访问登录/注册模式中的用户模式

type Query{
    hi: String!
}
type Mutation {
    register( userName: String!, email: String!, password: String! ): User
}

我正在使用来自graphql工具的模式 从merge-graphql-schema包中合并typeDefs和resolver

import "graphql-import-node";
import { GraphQLSchema } from "graphql";
import { mergeResolvers, mergeTypes } from "merge-graphql-schemas";
import * as userSchema from './modules/auth/shared/schema.graphql'
import * as registerTypedefs from "./modules/auth/register/schema.graphql";
import registerResolver from "./modules/auth/register/registerResolver";

import * as todoTypedefs from "./modules/todo/schema.graphql";
import todoResolver from "./modules/todo/todoResolver";

import { makeExecutableSchema } from "graphql-tools";

const schema: GraphQLSchema = makeExecutableSchema({
  typeDefs: mergeTypes([userSchema, registerTypedefs, todoTypedefs], { all: true }),
  resolvers: mergeResolvers([registerResolver, todoResolver])
});
export default schema;

我遇到了错误

Error: Field register: Couldn't find type User in any of the schemas.
    at collectNode (E:\graphql\express-apollo-server\node_modules\graphql-import\src\definition.ts:154:15)
    at E:\graphql\express-apollo-server\node_modules\graphql-import\src\definition.ts:135:7
    at Array.forEach (<anonymous>)

编辑 我已经使用了来自'merge-graphql-schemas'的fileLoader 而且效果很好

import { mergeResolvers, mergeTypes, fileLoader } from "merge-graphql-schemas";


const typesArray = fileLoader(path.join(__dirname, './modules/**/*.graphql'), { recursive: true })
const schema: GraphQLSchema = makeExecutableSchema({
  typeDefs: mergeTypes(typesArray, { all: true }),
  resolvers: mergeResolvers([registerResolver, loginRegister, todoResolver, storyResolver])
});

1 个答案:

答案 0 :(得分:1)

在我的项目中,我使用了require-graphql文件以及lodash合并,并且同样的情况对我有用。

import requireGraphQLFile from 'require-graphql-file';
import { merge } from 'lodash';

const $userSchema = requireGraphQLFile('./schema/yourFolder/userSchema.graphql');
const $registerTypedefs= requireGraphQLFile('./schema/yourFolder/register.graphql');

import registerResolver from "./modules/auth/register/registerResolver";
import todoResolver from "./modules/todo/todoResolver";

const schema: GraphQLSchema = makeExecutableSchema({
   typeDefs: [$userSchema, 
              $registerTypedefs,
              todoTypedefs
             ],              
   resolvers: merge(registerResolver, todoResolver)
});

希望这会有所帮助。