Apollo Federation
这是 GraphQL 库版本 6 的文档。对于长期支持 (LTS) 版本 5,请参阅 GraphQL 库版本 5 LTS. |
本指南介绍如何使用 Neo4j GraphQL 库创建子图,以将其组合到使用 Apollo Federation 的超图中。
目标是将以下单体模式转换为子图模式,以便在 Federation 网关后面使用
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL } from "@neo4j/graphql";
import neo4j from "neo4j-driver";
const typeDefs = `#graphql
type User @node {
id: ID!
name: String!
}
`;
const driver = neo4j.driver(NEO4J_URI, neo4j.auth.basic("username", "password"));
const neo4jGraphQL = new Neo4jGraphQL({
typeDefs,
driver,
})
const schema = await neo4jGraphQL.getSchema();
const server = new ApolloServer({
schema,
});
const { url } = await startStandaloneServer(server);
console.log(`🚀 Server ready at ${url}`);
设置
要继续进行转换,请按照以下步骤操作
-
为新项目创建一个目录,并
cd
到该目录mkdir neo4j-graphql-subgraph-example cd neo4j-graphql-subgraph-example
-
使用
npm
初始化一个新的 Node.js 项目npm init --yes npm pkg set type="module"
本操作指南使用 ES 模块设置项目,这允许使用顶层
await
等功能。 -
选择 TypeScript 或 JavaScript 以继续进行设置
-
创建一个包含空
index.js
文件的src
目录,以包含代码的入口点mkdir src touch src/index.js
-
将
package.json
文件中的默认scripts
条目替换为以下内容{ // ...etc. "scripts": { "start": "node index.js" } // other dependencies }
-
创建一个包含空 index.ts 文件的 src 目录,以包含代码的入口点
mkdir src touch src/index.ts
-
安装使用 TypeScript 项目所需的开发依赖项
npm install --save-dev typescript @types/node @tsconfig/node-lts
-
创建一个包含 TypeScript 编译器配置的空 tsconfig.json 文件
touch tsconfig.json
-
将以下配置添加到 tsconfig.json 文件中
{ "extends": "@tsconfig/node-lts/tsconfig.json", "compilerOptions": { "rootDir": "src", "outDir": "dist", } }
此配置扩展了由上面安装的 @tsconfig/node-lts 包提供的 Node.js LTS 的社区基础。有关可用选项的更多信息,请参阅 TypeScript 编译器文档。
-
将
package.json
文件中的默认scripts
条目替换为以下内容{ // ...etc. "scripts": { "compile": "tsc", "start": "npm run compile && node ./dist/index.js" } // other dependencies }
-
要继续进行设置,请运行以下命令安装所需的依赖项
npm install @apollo/server @neo4j/graphql graphql neo4j-driver
以下是需要安装的依赖项
-
@apollo/server
是 Apollo Server 库,本指南使用它来托管子图。 -
@neo4j/graphql
是 Neo4j GraphQL 库,它将 GraphQL 转换为 Cypher 并返回结果。 -
graphql
是 GraphQL 规范的参考实现。@neo4j/graphql
需要它才能正常运行。 -
neo4j-driver
是 Neo4j 驱动程序库,它是对数据库执行 Cypher 查询所需的库。
-
加入 Federation
为了使一组类型定义可用作 Federation 的子图,它们必须包含以下模式扩展
const typeDefs = `#graphql
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"])
type User @node {
id: ID!
name: String!
}
`;
此示例仅包含 Federation |
定义实体
将类型定义为 实体 允许其他子图向 Movie
类型贡献字段。为此,请使用 @key
指令指定字段(或字段)作为键
const typeDefs = `#graphql
extend schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"])
type User @key(fields: "id") @node {
id: ID!
name: String!
}
`;
虽然此示例中只添加了 @key
指令,但请考虑在 id
字段上使用 @id
或 @unique
指令。Federation 网关期望每个键解析为一个结果,因此建议确保这些值在数据库中是唯一的。
生成子图模式
使用 Neo4j GraphQL 库时,可以通过调用 getSubgraphSchema
而不是 getSchema
来生成子图模式。为此,需要更改以下行
const schema = neo4jGraphql.getSubgraphSchema();
结论
通过组合所有之前的代码片段,您应该得到以下内容
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { Neo4jGraphQL } from "@neo4j/graphql";
const typeDefs = `#graphql
type User @key(fields: "id") @node {
id: ID!
name: String!
}
`;
const driver = neo4j.driver("bolt://localhost:7687", neo4j.auth.basic("username", "password"));
const neo4jGraphQL = new Neo4jGraphQL({
typeDefs,
driver,
})
const schema = await neo4jGraphQL.getSubgraphSchema();
const server = new ApolloServer({
schema,
});
const { url } = await startStandaloneServer(server);
console.log(`🚀 Server ready at ${url}`);
为了进一步迭代,此子图也可以组合到超图中。查看 Apollo 的指南以获取更多说明