首页 > 解决方案 > 使用 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;

感谢您的任何提示

标签: node.jsneo4jgraphqlcypherapollo-server

解决方案


我发现,如果我为查询编写自定义解析器,Apollo 提供的指令不起作用。但是我意识到我可以让 Apollo 创建我需要的查询,所以我只是删除了我的自定义实现,这对我有用。

因此,在我的解析器中,我必须删除将获取带有@cypher 查询注释的字段的查询的实现,然后我可以将指令放入我的架构中并且它们运行良好。


推荐阅读