0

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;
    }
} 
atkayla
  • 8,143
  • 17
  • 72
  • 132

1 Answers1

0

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
}
helfer
  • 7,042
  • 1
  • 20
  • 20
  • Doesn't [Movie] expect the data to be an array of objects: [{id: 111, title:'Star Wars 1'}, {id: 112, title:'Star Wars 2'}]? I thought it would be better to store the Movies as an object with their ids as keys, so if I remove a movie by id would be O(1) instead of looping through the array to remove it O(n). Thoughts? Is there a way to do this with an object e.g. { Movie } instead? – atkayla Oct 05 '16 at 20:11
  • Your DB representation can be anything you want. It makes sense to store it as a list of IDs. But in the User.movies resolver, you can turn those into actual movie objects (but you don't have to) by fetching the movies for those IDs. The GraphQL schema shouldn't be influenced by how you store things in the DB, it should be mainly driven by the frontend needs. – helfer Oct 07 '16 at 02:57