In this article, I am going to briefly look at how to implement Neo4j and use GraphQl in my project. If you are not familiar with the mentioned concepts, I suggest you see Neo4j and GraphQl articles before reading this example.

Introduction

In this article, we use Java language and spring framework to implement. You can get the necessary tips for downloading, installing and confeg Neo4j from neo4j website. We also used Neo4j Browser software that is used to simplify data work. You can download versions tailored to your needs from the link.

Example Description

In this example, we want to show what vehicles each car company produces and what sponsorship each car has. Also, each separate sponsor can own a car.

Step 1

First, we add the required dependencies to our project.

  spring-boot-starter-data-neo4j
  neo4j-ogm-test
  neo4j-ogm-embedded-driver
  neo4j-java-driver-spring-boot-starter

Step 2

We specify our classes and @Node consider them as Node.

Company Class:

@Node
public class Company {

@Id
    private long id;
    private String name;

@Relationship(type = "OWN_IN", direction = Relationship.Direction.OUTGOING)
    private List<Car> carList;

public Company(long id, String name, List<Car> carList) {
        this.id = id;
        this.name = name;
        this.carList = carList;
    }

public long getId() {
        return id;
    }

public String getName() {
        return name;
    }

public List<Car> getCarList() {
        return carList;
    }
}</Car></Car></Car>

Car Class:

@Node
public class Car {

@Id
    private long id;
    private String name;
    private int year;

@Relationship(type = "OWN_ENGINE", direction = Relationship.Direction.OUTGOING)
    private List<Engine> engines;

public Car(long id, String name, int year, List<Engine> engines) {
        this.id = id;
        this.name = name;
        this.year = year;
        this.engines = engines;
    }

public long getId() {
        return id;
    }

public String getName() {
        return name;
    }

public int getYear() {
        return year;
    }

public List<Engine> getEngines() {
        return engines;
    }
}</Engine></Engine></Engine>

And sponsor class:

@Node
public class Sponsor {

@Id
    private long id;
    private String name;

@Relationship(type = "OWN_CAR", direction = Relationship.Direction.OUTGOING)
    private List<Car> carList;

public Sponsor(long id, String name, List<Car> carList) {
        this.id = id;
        this.name = name;
        this.carList = carList;
    }

public long getId() {
        return id;
    }

public String getName() {
        return name;
    }

public List<Car> getCarList() {
        return carList;
    }
}</Car></Car></Car>

Step 3

In this step, we try to apply CRUD with in-app data.

@Repository
public interface CarRepository extends Neo4jRepository<Car, string=""> {
    
Car findByName(String name);

@Query("MATCH(n:Car START WITH {$pattern}) RETURN n")
    List<Car> findByPatternName(@Param("pattern") String pattern);
}</Car></Car,>

As you can see, you can use two methods to find a ninety or a list of ninety. The first method is to use the possibility that Neo4j has given us and only by inserting the function name so findByName finds ninety targets with its name input. If two nineties have the same name, it restores the first ninety found. The second method is to use @Query. In this method, which is more accurate and complete, we can find our 90 goals. In the following articles, we will talk about queries in Neo4j.

To implement other actions, such as adding ninety and deleting it, we use save and delete functions in the service layer. For a better understanding, please see the code snippet below.

@Service
public class CarService {
    
@Autowired
    private CarRepository carRepository;
    
private void add(
            long id,
            String name,
            int year,
            List<Sponsor> sponsors
    ){
        Car car = new Car(
                id,
                name,
                year,
                sponsors
        );
        
carRepository.save(car);
    }

private void delete(String id){

carRepository.deleteById(id);
    }
}</Sponsor>

You can get the relevant project from the GitHub link.

And in the end,

Mirbazorgi’s website aims to help you learn and fix your problems by providing articles and practical experiences. Email me if you have any questions.