简体   繁体   中英

Can't use @cypher in GraphQL schema when using ApolloWebserver

I want to query a field on a node using the @cypher directive in my GraphQL schema.

However when I query the field I get Resolve function for \"Link.x\" returned undefined .

My schema with the directive on x from Link is the following

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
}

When querying for aa link and its x property I get undefined. With the custom resolver that I wrote for y however it works. Of course I could leave the hand written resolvers but its a lot of code that is not necessary.

This is 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 }` );
} );

With "graphql-schema.js" being

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;

Thanks for any tips

I found out that, if I write a custom resolver for a query, the directives provided by Apollo do not work. However I realized that I can let Apollo create the queries that I needed so I just deleted my custom implementations, which works for me.

So in my resolvers I had to remove implementations for queries that would fetch fields annotated with a @cypher query, then I could put the directive into my schema and they worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM