Search code examples
graphqlapolloapollo-serverapollostack

What does the GraphQL schema/resolver look like for an object of objects?


data

{
    user_id: 'abc',
    movies: {
        '111': {
            title: 'Star Wars 1' 
        },
        '112': {
            title: 'Star Wars 2' 
        }
    }
}

What would the schema and resolver for this look like?

This was my best attempt, but I've never seen an example like this, so really not sure.

schema

type User {
    user_id: String
    movies: Movies
}
type Movies {
    id: Movie
}
type Movie {
    title: String
}

resolver

User: {
    movies(user) {
        return user.movies;
    }
},
Movies: {
    id(movie) {
        return movie;
    }
} 

Solution

  • You're still missing a Query type, which tells GraphQL where your queries can start. Something like:

    type Query {
      user(id: String): User
      movie(id: String): Movie
    }
    

    also, I think for your movies, you should have [Movie] instead of a Movies type and then id inside of it. So get rid of your Movies type and just do this:

    type User {
        id: String
        movies: [Movie]
    }
    type Movie {
        id: String
        title: String
    }