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();
},
}
}