Search code examples
neo4jgraphqlcypherload-csv

session.run(LOAD CSV) issues


When using LOAD CSV function with session.run() to execute cypher statements into Neo4J it doesn't return anything. Have tried removing LOAD CSV and it works perfectly fine in creating nodes.

This is the code:

import { neo4jgraphql } from "neo4j-graphql-js";
import fs from "fs";
import path from "path";
const {createWriteStream} = require("fs");
const {GraphQLUpload} = require("apollo-server");
import {session} from "./index.js"

/*
 * Check for GRAPHQL_SCHEMA environment variable to specify schema file
 * fallback to schema.graphql if GRAPHQL_SCHEMA environment variable is not set
 */
const files = [];
export const typeDefs = fs
  .readFileSync(
    process.env.GRAPHQL_SCHEMA || path.join(__dirname, "schema.graphql")
  )
  .toString("utf-8");

export const resolvers = {
//  Upload: GraphQLUpload,
  Query: {
    files: () => files
  },
  Mutation: {
    uploadFile: async (_, {file}) => {
      const {createReadStream, filename } = await file;

      await new Promise(res =>
        createReadStream()
          .pipe(createWriteStream(path.join(__dirname, "./uploads", filename)))
          .on("close", res)
        );
        session.run("LOAD CSV WITH HEADERS FROM 'file:///Connections.csv' AS csvLine CREATE(n)"); // This does not work with the LOAD CSV (Works with only CREATE(n))
        return true;
    }
  }
};


Solution

  • That LOAD CSV command will just invoke CREATE (n) once per line which will create a node with no label or properties. Try changing it to something like CREATE (n:CsvNode) SET n.prop_1 = csvLine.header1 (where header1 is one of the headers from your CSV) and see if nodes labelled :CsvNode get created.