环境详情:
"@neo4j/graphql": "^3.12.0",
"@neo4j/introspector": "^1.0.2",
"apollo-server": "^3.11.1",
"dotenv": "^16.0.3",
"graphql": "^16.6.0",
"neo4j-driver": "^4.4.3"
OS : Windows10
graphql 文件中有 201 个类型定义和 105 个接口定义。 neo4j graphql 生成大小约为 26 MB 的 SDL 文件(SDL 中的类型定义约为 8002)
问题是,一个简单的查询大约需要 10-15 分钟才能得到响应,有时我们甚至没有得到响应。 下面是启动服务器的代码片段
const server = new ApolloServer({
schema: schema,
introspection: true,
playground: true,
});
但是,如果我们使用较少数量(~20-25)的类型定义,那么它会按预期工作。
graphql 中类型定义的数量有大小限制吗?
要了解发生了什么,您可以使用带有 env 变量的 Neo4j 节点调试器
DEBUG=@neo4j/graphql:* tsx --watch src/index.ts
您将看到生成的查询。
当您使用接口时,每次您尝试访问接口类型时,neo4j 都会在后台为继承该接口的每个类型生成一个带有 UNION 的查询,以便它可以正确处理类型条件(即使您不请求它们) .
这意味着如果你有一个接口
type A {
id: ID
}
有10个子类型
A1 extends A
到A10 extends A
,查询
query {
As(where: { id: "123" }) {
id
}
}
将生成以下形式的查询:
CALL {
MATCH (this0:A1 {id: $param0})
WITH this0 { .id, __resolveType: "A1", __id: id(this0) } AS this0
RETURN this0 AS this
UNION
MATCH (this1:A2 {id: $param1})
WITH this1 { .id, __resolveType: "A2", __id: id(this1) } AS this1
RETURN this1 AS this
UNION
...
MATCH (this9:A10 {id: $param9})
WITH this9 { .id, __resolveType: "A10", __id: id(this9) } AS this9
RETURN this9 AS this
}
WITH this
RETURN this as this
10 个参数全部相同。
这个包含大量联合的查询非常慢,比运行 10 倍的查询慢得多。
据我所知,到目前为止还没有已知的解决方案。
即使没有带来解决方案,我希望这能解开性能问题的谜团