Skip to content

collection.find(...).toArray is not a function? #8

@GimignanoF

Description

@GimignanoF

I've implemented the example code, however when the resolver hits the data source, this error pops up

TypeError: collection.find(...).toArray is not a function
      at _dataloader.default.ids (E:\test-graphql\node_modules\apollo-datasource-mongodb\dist\cache.js:31:6)    
      at dispatchQueueBatch (E:\test-graphql\node_modules\apollo-datasource-mongodb\node_modules\dataloader\index.js:246:22)
      at dispatchQueue (E:\test-graphql\node_modules\apollo-datasource-mongodb\node_modules\dataloader\index.js:233:5)
      at E:\test-graphql\node_modules\apollo-datasource-mongodb\node_modules\dataloader\index.js:70:20
      at process._tickCallback (internal/process/next_tick.js:61:11)

This is how I create the data source in it's own file

const { MongoDataSource } = require("apollo-datasource-mongodb");

class ProductPages extends MongoDataSource {
  getProductPage(id) {
    return this.findOneById(id);
  }
}

exports.ProductPagesDataSource = new ProductPages(ProductPageModel);

This is how I add it to the ApolloServer

const { ProductPagesDataSource } = require("../graphql/schema/schema");
const app = new ApolloServer({
  ...
  dataSources: () => ({
    productPages: ProductPagesDataSource
  })
});

And in the resolver I call it like this (I use graphql-compose with graphql-compose-mongoose for my schema, but that shouldn't be an issue since I can make it work using the DataLoader package)

schemaComposer.Query.addFields({
  page: ProductPageSchema.get("$findById").wrapResolve(next => rp => {
    return rp.context.dataSources.productPages.getProductPage(rp.args._id);
  })
});

What am I doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions