首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apollo Server模式拼接使用mergeSchemas,如何从单独的模式中使用类型?

Apollo Server模式拼接使用mergeSchemas,如何从单独的模式中使用类型?
EN

Stack Overflow用户
提问于 2019-01-29 08:07:16
回答 1查看 656关注 0票数 2

我目前正在将使用graphql-js定义的模式迁移到使用typedefs和解析器的executableSchema。我想迭代地迁移我的代码库,并将它们缝合在一起。因此,让一些部分仍然使用graphql-js,而另一些部分使用gql typedefs,直到我开始研究整个模式。

当我试图在我的typedefs中重用现有类型时,我遇到了一个问题。

例如,我的typedef可能是:

代码语言:javascript
复制
type Query {
  customers: [User]
}

但是在我的graphql-js模式中存在User类型的定义。因此,即使在模式被缝合之后,它也会显示为Error: Type "User" not found in document

这是可能的吗?或者我必须一次迁移所有相关的内容吗?

示例:

代码语言:javascript
复制
// The original schema is created using graphql-js 
const oldSchema = new GraphQLSchema({query: ..., mutation: ...});

// The parts I'm slowly migrating to use graphql-tools
const newSchema = makeExecutableSchema({ typeDefs: ..., resolvers: ...});

// Then putting them together
const mergedSchema = mergeSchemas({schemas: [oldSchema, newSchema]})
EN

回答 1

Stack Overflow用户

发布于 2019-02-01 22:47:39

我不确定这是否适用于你的“旧模式”,但在我的项目中,我用以下方式合并了它们……

代码语言:javascript
复制
import requireGraphQLFile from 'require-graphql-file';
import {makeExecutableSchema } from 'apollo-server';

const schema1= requireGraphQLFile('./schema/schema1');
const schema2= requireGraphQLFile('./schema/schema2');
const schema3= requireGraphQLFile('./schema/schema3');

const schema = makeExecutableSchema({
   typeDefs: [schema1,schema2, schema3],
   resolvers
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54412079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档