Apollo MongoDB Datasource Help

Hello Guys? I have an issue with my code and you certainly know how to solve it. The problem is when i make a getCrypto query its returning nulls values. But my getCryptos function is working fine.
I would like to query my cryptos with with “code” param. For example. When i try to query with [code: “BTC”] variable im getting nulls results. I think the problem is with resolver or datasource. I just would like to query all results by code. Thank you.

ApolloServer

const schema = application.createSchemaForApollo();

    const apolloServer = new ApolloServer({ 
      schema,
      dataSources: () =>({
        cryptoAPI: new Cryptos(client.db('cryptoDB').collection('cryptos'))
      }),
      csrfPrevention: true,
      introspection: true,
      plugins: [
        // Install a landing page plugin based on NODE_ENV
        process.env.NODE_ENV === 'production'
          ? ApolloServerPluginLandingPageProductionDefault({
              graphRef: "Finbrothers@current",
              footer: false,
            })
          : ApolloServerPluginLandingPageLocalDefault(),
      ],
    })

My Datasource


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

export default class Cryptos extends MongoDataSource {
    getCrypto(code) {
      return this.findByFields(code);
      }

    getCryptos(fields = {}) {
        return this.findByFields(fields);
    
    }
}

My resolver


import { GraphQLScalarType } from 'graphql';

const dateScalar = new GraphQLScalarType({
  name: 'Date',
  description: 'Date custom scalar type',
  parseValue(value) {
    return new Date(value); // Convert incoming integer to Date
  },
});


export const CryptoResolver = {

    Date: dateScalar,
  
    Query: {
            getCrypto: (_, {code} , { dataSources }) => {
                return dataSources.cryptoAPI.getCrypto(code);

            },
            getCryptos: (_, __, { dataSources }) => {
                return dataSources.cryptoAPI.getCryptos();
                
            },
    }
}

Is your field called code in the datasource? if yes maybe

    getCrypto(code) {
        return this.findByFields({ code } ); // instead of this.findByFields(code);
      }

Hope it helps.

Hi Kheang. I did what you suggest but still the same null response like my printscreen.

What does your DB look like?

Do you mean my DB schema?

Try to do some debugging.
console.log when you call getCrypto the code and see what is passed to the findByFields.
Try to hardcode findById with the actual _id of BTC to see if it pulls it correctly.

If I look at it rapidly, it looks fine, must be something that is not sent properly to your datasource maybe.

Add try / catch to see if you get any exceptions that aren’t displayed if you get to this point.

Hi Kheang. I finally found the bug. Everything was fine as you said. The problem was with Type query. I forgot to use on getCrypto. The wrong one was “getCrypto(code: String): Crypto” and the correct was getCrypto(code: String): [Crypto]. Just to community knows. Thanks again.

1 Like

Ahhhhhhh because it was findByFields which returns an array.
You could’ve kept `getCrypto(code: String): Crypto and just modify your resolver to return the first element it finds.

I didn’t catch that either!