Merging resolvers from neo4j-graphql-js and regular datastore don't work

javascript
graphql
(Npatel) #1

Hi,

I have a graph network with minimum info about products and their relations. All the detail information is in mysql database and available via a REST api call.

MY Schema:

  type Product {
    id: ID
    name: String   
    details: ProductDetails
  }
  
  type ProductDetails {
    product_id: ID
    product_name: String
    release_date: String,
    status: String
  }
  type  Query {  
    mergeProduct(id: Int!): Product
  }

and Resolvers

Query: {
    mergeProduct(object, params, ctx, resolveInfo) {
      return neo4jgraphql(object, params, ctx, resolveInfo, true);
    }
}
Product: {
    details: async (resolvedObj, params, ctx) => {
      console.log("in product_id resolver for id=", resolvedObj.id);
      obj = await ctx.dataSources.productDetailsApi.getProduct(resolvedObj.id);
      console.log("resolver got ", obj);
      return obj;
    }
  },

Request:

{
  mergeProduct(id: 1784029){
  	id
        name
        details {
           product_id
       }
  }
}

It works when I don't request details. But this gives me error Error: "Cannot read property '0' of undefined"

Is there a better way to do this?

Thanks.

0 Likes

(William Lyon) #2

Try adding the @neo4j_ignore directive to the details field in your typedefs. This directive will ensure that field is explicitly ignored by neo4j-graphql.js.

Here's a very similar example with a mocked up resolver (I just changed Product --> Business so it would work with an example database I have) running in CodeSandbox: https://codesandbox.io/s/0on871118p

Try this GraphQL Query:

{
  Business(first:1) {
    name
    details {
      product_id
      product_id
      release_date
      status
    }
  }
}

and the code

const { ApolloServer } = require("apollo-server");
const { makeAugmentedSchema } = require("neo4j-graphql-js");
const { v1 } = require("neo4j-driver");

// Construct a schema, using GraphQL schema language
const typeDefs = `
type Business {
  name: String
  details: ProductDetails @neo4j_ignore
} 

type ProductDetails {
  product_id: ID
  produt_name: String
  release_date: String
  status: String
}

`;

const resolvers = {
  Business: {
    details: (obj, params, ctx, resolveInfo) => {
      return {
        product_id: "2342lkajldf",
        produt_name: "Bob Loblaw",
        release_date: "12/10/2019",
        status: "pending"
      };
    }
  }
};

// Generate executable schema with auto-generated resolvers
const schema = makeAugmentedSchema({ typeDefs, resolvers });

// Instantiate a Neo4j database driver
const driver = v1.driver(
  "bolt+routing://d2e2ad3f.databases.neo4j.io",
  v1.auth.basic("reviews", "reviews")
);

// Create a new ApolloServer, injecting the database driver
// into the context
const server = new ApolloServer({
  context: { driver },
  schema
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

1 Like