question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this.findOneById is not a function (Apollo Server Express v4 GraphQL)

See original GitHub issue

I have the following ApolloServer (v4)

import { MongoDataSource } from 'apollo-datasource-mongodb'

export default class LoaderAsset extends MongoDataSource {
 async getAsset(assetId) {
    return this.findOneById(assetId) // ERROR IS HERE
  }
}

async function startApolloServer(app) {
  const httpServer = http.createServer(app);
  
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer })]
    
  });

  await server.start();
  app.use(
    '/graphql',
    cors(),
    bodyParser.json(),
    expressMiddleware(server, {
      context: async ({ req }) => {
       return {
        dataSources: {
          loaderAsset: new LoaderAsset(modelAsset),
        }
      }
      },
    }),
  );


  const port = config.get('Port') || 8081;
  await new Promise(resolve => httpServer.listen({ port }, resolve));
}

when I run graphql and send one assetId, everything is working till I get following error:

this.findOneById is not a function

By the way (this.) has collection and model objects but not any methods.

is it because apollo-datasource-mongodb is not compatible with the new version of apollo server v4?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
systemkrashcommented, Oct 20, 2022

what i did is i override my datasource class so I can call the initialize method inside in the MongoDatasource class. Now it works for me.

import { MongoDataSource } from 'apollo-datasource-mongodb';

class ReceptionDataSource extends MongoDataSource {
  constructor({ collection, cache }) {
    super(collection);
    super.initialize({ context: this.context, cache });
  }

  async getReception(receptionId) {
    return await this.findOneById(receptionId);
  }
}

export default ReceptionDataSource;

then in my context

async function startApolloServer(app) {
  const httpServer = http.createServer(app);
  
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer })]
    
  });

  await server.start();
  app.use(
    '/graphql',
    cors(),
    bodyParser.json(),
    expressMiddleware(server, {
      context: async ({ req }) => {
      const { cache } = server
       return {
        dataSources: {
          loaderAsset: new ReceptionDataSource({collection: ReceptionModel, cache}),
        }
      }
      },
    }),
  );


  const port = config.get('Port') || 8081;
  await new Promise(resolve => httpServer.listen({ port }, resolve));
}
0reactions
systemkrashcommented, Nov 9, 2022

@systemkrash Thank you for your code snippet, it’s of great help! I’m still struggling with a point: How do you correctly set the context in the DataSource (with additional fields)?

super.initialize({ context: this.context, cache });

this.context is always undefined, isn’t it?

Also I’ll have a problem, even with a defined context. I was exploiting the fact that the dataSources context was containing the other dataSources and himself:

// inside any of my DataSource extended class

this.context.dataSources.users.findOneById(this.context.token)

EDIT: I got help from a member of ApolloGraphQL and found a way to set correctly the context and dataSources: apollographql/apollo-server#7096

I believed its not undefined because that attribute belongs to MongoDataSource class which we use to extend our own Datasource class. sorry for the late response.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apollo Server Express v4 GraphQL this.findOneById is not a ...
when I run graphql and send one assetId, everything is working till I get following error: this.findOneById is not a function By the...
Read more >
Issues · GraphQLGuide/apollo-datasource-mongodb - GitHub
Contribute to GraphQLGuide/apollo-datasource-mongodb development by creating an account ... findOneById is not a function (Apollo Server Express v4 GraphQL).
Read more >
Using Express with GraphQL – How to create a GraphQL ...
One of the fastest ways to get up and running with GraphQL is to install Apollo Server as middleware on your new or...
Read more >
Graphql-typegraphql example to apollo/server/express4 - help
Hello, I wanted to change the example code in src/index.ts to use apollo/server/express4 in prisma-examples/typescript/graphql-typegraphql ...
Read more >
create-graphql-server-query-arguments - npm package | Snyk
Learn more about create-graphql-server-query-arguments: package health ... apollo-server-express ... findOneById(id, me, 'pubsub {{typeName}}Inserted'); ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found