@ apollo/ 子图
阿波罗联邦 graphql-js 生态系统.
@apollo/subgraph 建在顶部 graphql-js 并提供了转换逻辑
使您的 GraphQL 计程器兼容 . buildSubgraphSchema 添加常见
联合会类型定义(如:. Any 斯卡尔, _Entity 工会、联邦指令、,
,并允许您简单指定您的联邦实体解析器.
安装
要求节点.js 22或后.
npm install @apollo/subgraph graphql
使用量
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { buildSubgraphSchema } from '@apollo/subgraph';
import gql from 'graphql-tag';
const typeDefs = gql`
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key"])
type Query {
product(id: ID!): Product
}
type Product @key(fields: "id") {
id: ID!
name: String
}
`;
const resolvers = {
Query: {
product: (_source, { id }) => products.find((product) => product.id === id),
},
Product: {
// Called for each entity representation the router sends to `_entities`.
__resolveReference: (reference) =>
products.find((product) => product.id === reference.id),
},
};
const server = new ApolloServer({
schema: buildSubgraphSchema([{ typeDefs, resolvers }]),
});
await startStandaloneServer(server, { listen: { port: 4001 } });
API 密码
buildSubgraphSchema(modulesOrSDL)——构建可执行子图计划. 接受一个DocumentNode,或一系列文档或{ typeDefs, resolvers }模块.printSubgraphSchema(schema)印出完整的子图计划, 包括联邦指令和类型.addResolversToSchema(schema, resolvers)将解析器地图附在 现有计划、理解__resolveReference和平常一样__resolveType编号 :__isTypeOf。 。 。 .entitiesResolver({ representations, context, info })- 那个_entities解析器,导出给集合自己根字段的库.
实体参考资料通过 __resolveReference或来自
解析器映射或从 extensions.apollo.subgraph.resolveReference 在类型.
发展
npm install
npm run build
npm test
这个 compatibility/ 工作空间运行
阿波罗联邦子图兼容套件
针对这个库,使用道克来作路由器和参考子图.
见其 读取。 。 。 .
npm run compatibility --workspace @apollo/subgraph-compatibility
联系人
如果您对库或代码有特定问题,请在 阿波罗社区论坛。 。 。 .
捐款
见 捐款额 详细情况.
在你建立你的当地分支后,看看我们的公开问题,看看你们可以在哪里作出贡献.
警卫
有关如何联系小组处理安全问题的更多信息,见我们 安全政策。 。 。 .
许可证
这个图书馆的许可依据是 《麻省理工学院许可证》。 。 。 .