Use GraphQLList with GraphQLObject Types

Share this video with your friends

Send Tweet

When working with collections of things in GraphQL, we'll always reach out for the GraphQLList Type. In this video, we'll learn how to use GraphQLList from the graphql package in combination with a GraphQLObject Type to create a field that returns a collection in our Schema.

Brendan Whiting
Brendan Whiting
~ 6 years ago

If I pass in the getVideos function as we did here, resolve: getVideos, I get this error. "message": "resolveFn is not a function". But if construct it like with did the other resolver, it works: resolve: (_, args) => getVideos()`. But I still get this error in GraphiQL even though it's working: 'Cannot query field 'videos' on type "QueryType". Did you mean 'video'?

const queryType = new GraphQLObjectType({
  name: 'QueryType',
  description: 'The root query type.',
  fields: {
    videos: {
      type: new GraphQLList(videoType),
      resolve: (_, args) => getVideos()
    },
    video: {
      type: videoType,
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLID),
          description: 'The id of the video.'
        }
      },
      resolve: (_, args) => getVideoById(args.id)
    }
  },
});
Brendan Whiting
Brendan Whiting
~ 6 years ago

...nevermind, i was just defining the getVideos function wrong...