我们如何从 apollo-server 模块传递 ApolloServer 中的数据源和合并的 graphql 模式和解析器?
Posted
技术标签:
【中文标题】我们如何从 apollo-server 模块传递 ApolloServer 中的数据源和合并的 graphql 模式和解析器?【英文标题】:How can we pass datasource and merged graphql schema and resolvers in ApolloServer from apollo-server module? 【发布时间】:2019-05-29 06:31:16 【问题描述】:我有一种情况,我的架构和解析器位于各种不同的文件中。我已将架构和解析器导入到负责创建 apollo 服务器的 server.js 文件中。目前,我正在导入一组解析器和查询,而 server.js 文件中已经存在另一组查询和解析器。
这是 server.js 文件的内容。
import "graphql-import-node";
import ApolloServer, gql from "apollo-server";
import makeExecutableSchema from "graphql-tools";
import merge from "lodash/merge";
import GithubFetchHelper from "./github/GithubFetchHelper";
import
resolvers as GithubResolvers,
typeDefs as GithubTypeDef
from "./github";
/**
* Error Handler. Provides full stack - remove for production
*/
const books = [
title: "Harry Potter and the Chamber of Secrets",
author: "J.K. Rowling"
,
title: "Jurassic Park",
author: "Michael Crichton"
];
const typeDefs = gql`
# Comments in GraphQL are defined with the hash (#) symbol.
# This "Book" type can be used in other type declarations.
type Book
title: String
author: String
# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query
books: [Book]
`;
const resolvers =
Query:
books: () => books
;
// typeDefs: [
// project.typeDefs,
// task.typeDefs,
// search.typeDefs
// ].join(' '),
// resolvers: merge(, project.resolvers, task.resolvers, search.resolvers),
// context:
// models:
// project: project.model,
// task: task.model
// ,
// loaders: loaders()
//
const server = new ApolloServer(
typeDefs: [typeDefs, GithubTypeDef],
resolvers: merge(, resolvers, GithubResolvers),
dataSources: () =>
return
gitHubApi: new GithubFetchHelper()
;
);
server.listen().then(( url ) =>
console.log(`???? Server ready at $url`);
);
export default server;
现在当我运行服务器时,我收到以下错误。任何帮助表示赞赏。
Error: Type "Query" was defined more than once.
我的尝试我参考了 apollo 文档以获取数据源以及使用来自 graphql-tools 的 gql 合并多个模式和解析器。但是我没有得到任何 gql 的实现,他们也使用了数据源。
感谢任何帮助。
注意:我对 Graphql 很陌生
【问题讨论】:
您在哪里能够弄清楚如何实现这一目标..? 【参考方案1】:从代码中,您将 2 个 typedef 作为数组传递,这可能是您收到错误的原因。您需要将架构缝合在一起。见:
https://www.graphql-tools.com/docs/schema-stitching/stitch-combining-schemas https://blog.apollographql.com/the-next-generation-of-schema-stitching-2716b3b259c0【讨论】:
【参考方案2】:假设您在主 js 文件中导入了架构和解析器,您可以按以下方式拼接它。
您可以使用 require-graphql-file
模块来简化 graphQL 架构导入。
import merge from 'lodash'
import requireGraphQLFile from 'require-graphql-file';
import makeExecutableSchema from 'apollo-server';
import resolver1 from './resolvers/resolver1';
import resolver2 from './resolvers/resolver2';
import resolver3 from './resolvers/resolver3';
const schema1= requireGraphQLFile('./schema/schema1');
const schema2= requireGraphQLFile('./schema/schema2');
const schema3= requireGraphQLFile('./schema/schema3');
const resolvers = merge(resolver1, resolver2, resolver3)
const typeDef = gql `
type Query
getData(filters:Filter,limit:Int,offset:Int):Result
`;
const schema = makeExecutableSchema(
typeDefs: [typeDef, schema1,schema2, schema3],
resolvers
);
const server = new ApolloServer(
schema
);
【讨论】:
以上是关于我们如何从 apollo-server 模块传递 ApolloServer 中的数据源和合并的 graphql 模式和解析器?的主要内容,如果未能解决你的问题,请参考以下文章
我如何通过 nodejs 通过 apollo-server 从服务器获取所有数据
找不到模块:无法解析“apollo-link-context”-REACT,APOLLO-SERVER
在 sequelize 和 apollo-server 中从目标解析源关联时理解“包含”