node.js - 使用 ApolloWebserver 时无法在 GraphQL 架构中使用 @cypher
问题描述
我想在我的 GraphQL 架构中使用 @cypher 指令查询节点上的字段。
但是,当我查询我得到的字段时Resolve function for \"Link.x\" returned undefined
。
我的带有来自 Link 的 x 指令的架构如下
scalar URI
interface IDisplayable{
"Minimal data necessary for the object to appear on screen"
id: ID!
label: String
story: URI
}
interface ILink{
"""
A link must know to what nodes it is connected to
"""
x: Node! @cypher(statement: "MATCH (this)-[:X_NODE]->(n:Node) RETURN n")
y: Node!
"""
if optional=true then sequence MAY be used to define a set of options
"""
optional: Boolean
}
interface INode{
synchronous: Boolean
unreliable: Boolean
}
type Node implements INode & IDisplayable{
id: ID!
label: String!
story: URI
synchronous: Boolean
unreliable: Boolean
}
type Link implements ILink & IDisplayable{
id: ID!
label: String!
x: Node! @cypher(statement: "MATCH (this)-[:X_NODE]->(n:Node) RETURN n")
y: Node!
story: URI
optional: Boolean
}
当查询 aa 链接及其 x 属性时,我得到未定义。使用我为 y 编写的自定义解析器,它可以工作。当然,我可以留下手写的解析器,但它有很多代码是不必要的。
这是 index.js:
require( 'dotenv' ).config();
const express = require( 'express' );
const { ApolloServer } = require( 'apollo-server-express' );
const neo4j = require( 'neo4j-driver' );
const cors = require( 'cors' );
const { makeAugmentedSchema } = require( 'neo4j-graphql-js' );
const typeDefs = require( './graphql-schema' );
const resolvers = require( './resolvers' );
const app = express();
app.use( cors() );
const URI = `bolt://${ process.env.DB_HOST }:${ process.env.DB_PORT }`;
const driver = neo4j.driver(
URI,
neo4j.auth.basic( process.env.DB_USER, process.env.DB_PW ),
);
const schema = makeAugmentedSchema( { typeDefs, resolvers } );
const server = new ApolloServer( {
context: { driver },
schema,
formatError: ( err ) => {
return {
message: err.message,
code: err.extensions.code,
success: false,
stack: err.path,
};
},
} );
const port = process.env.PORT;
const path = process.env.ENDPOINT;
server.applyMiddleware( { app, path } );
app.listen( { port, path }, () => {
console.log( `Server listening at http://localhost:${ port }${ path }` );
} );
使用“graphql-schema.js”
const fs = require( 'fs' );
const path = require( 'path' );
const schema = './schemas/schema.graphql';
const encoding = 'utf-8';
let typeDefs = '';
typeDefs += fs.readFileSync( path.join( __dirname, schema ) )
.toString( encoding );
module.exports = typeDefs;
感谢您的任何提示
解决方案
我发现,如果我为查询编写自定义解析器,Apollo 提供的指令不起作用。但是我意识到我可以让 Apollo 创建我需要的查询,所以我只是删除了我的自定义实现,这对我有用。
因此,在我的解析器中,我必须删除将获取带有@cypher 查询注释的字段的查询的实现,然后我可以将指令放入我的架构中并且它们运行良好。
推荐阅读
- cryptojs - 错误[E0463]: 找不到 `core` 的 crate | = 注意:可能未安装“x86_64-fortanix-unknown-sgx”目标
- python - Python:以相反的顺序打印输入(条件:只有单词(至少4个单词))
- python - 我什么时候应该在 tkInter 中为 python 使用 root.update()
- r - 使用 nls 处理 Sapply 循环中的错误 - 循环在第一个错误处停止并且不输出任何数据
- jquery - 克隆表的选择器?表2excel
- python - NameError:未定义名称“uuid_generate_v4”
- java - Quartz API - 如何从字符串中的 cron 条目输出秒数?
- mapbox-ios - iOS上的Mapbox - 根据源层中的属性动态绘制图标
- javascript - SVG PacMan 中的鬼眼在 Firefox 中正确渲染,但在其他浏览器中不正确
- c++ - 无法将值分配给堆栈