Search code examples
javaspring-bootgraphqlgraphql-java

Could not initialize proxy with graphql-spring


I am using: graphql-spring-boot and graphql-java-tools for the implementation.

movie.graphqls

type Movie {
    id: Short
    name: String
    poster: String
    releaseDate: String
    runtime: String
    storyline: String
    rated: String
    rating: String
    inserted: String
}


type Query {
    movies: [Movie]
    movie(id: ID!): Movie
}

Movie model

@Entity
public class Movie {
    private Short id;
    private String name;
    private String poster;
    private Date releaseDate;
    private Time runtime;
    private String storyline;
    private String rated;
    private double rating;
    private Timestamp inserted;
}

As you can see i have no relationship with other models.
finally the class which implement GraphQLQueryResolver

@Component
public class Query implements GraphQLQueryResolver {
    @Autowired
    private MovieRepository movieRepository;
    public List<Movie> movies() {
        return this.movieRepository.findAll();
    }
    public Movie movie(Short id) {
        return this.movieRepository.getOne(id);
    }
}

the following query works fine:

{
  movies{
    name
    rating
  }
}

but this query:

{
  movie(id: 1){
    name
  }
}

gives me the following error:

Exception while fetching data (/movie/rated) : could not initialize proxy [com.example.demo.model.Movie#1] - no Session


Solution

  • Try changing getOne to findOne

    public Movie movie(Short id) {
        return this.movieRepository.findOne(id);
    }
    

    I think using getOne requires the call to be within a transaction context. Hence the error - no session.