我们定义了一个带有两个ApolloClient的ApolloLinks,并与MongoDB和PostgreSQL连接,并且它工作得很好:
const firstLink = new HttpLink({
uri: 'graphql-postgre',
//headers: yourHeadersHere,
// other link options...
});
const secondLink = new HttpLink({
uri: 'graphql-mongodb',
//headers: yourHeadersHere
// other link options...
});
const client = new ApolloClient({
link: ApolloLink.split(
o => o.getContext().clientName === "mongo",
secondLink,
firstLink // by default -> postgre)
),
cache: new InMemoryCache(),
fecthOptions: {
mode: 'no-cors'
},
shouldBatch: true
});
现在,我们需要添加一个新的链接来访问一个新的数据库(Neo4J),但是我们找不到任何例子,我们也不知道是否可以使用两个以上的源。我们尝试了下面的代码,试图在第二个链接中包含一些逻辑,但是它并不像我们预期的那样工作。我们从第一个和第二个链接获得信息,但从第三个链接中得不到信息:
const thirdLink = new HttpLink({
uri: 'graphql-neo4j',
//headers: yourHeadersHere
// other link options...
});
const client = new ApolloClient({
link: ApolloLink.split(
o => o.getContext().clientName === "mongo",
secondLink,
(o => o.getContext().clientName === "neo",
thirdLink,
firstLink) // by default -> postgre)
),
cache: new InMemoryCache(),
fecthOptions: {
mode: 'no-cors'
},
shouldBatch: true
});
提前谢谢你。
发布于 2022-02-21 08:16:24
不幸的是,ApolloLink.split只允许两个选项,但是您仍然可以使用这种方法绕过这个限制。
const client = new ApolloClient({
link: ApolloLink.split(
(o) => o.getContext().clientName === 'mongo',
secondLink,
ApolloLink.split((o) => o.getContext().clientName === 'neo',
thirdLink,
firstLink)
), // by default -> postgre)
cache: new InMemoryCache(),
fecthOptions: {
mode: 'no-cors',
},
shouldBatch: true,
});
https://stackoverflow.com/questions/67077935
复制