Search code examples
typescriptapolloapollo-server

Error setting up Apollo server - Type '() => void' is not assignable to type '() => DataSources<object>'


I know it is typescript related. The function dataSources below is expecting a return of a certain type and I am not sure how to implement in this situation

Here is the code -

const dataSources = () => {
    quizzessApi: new QuizzessDataSource();
}

const server = new ApolloServer({ typeDefs, resolvers, dataSources  });
export default server.createHandler();

This is the error I see on vscode -

typescript error message

Updated with the QuizzesDataSource class -

import { DataSource } from "apollo-datasource";

export interface quizzessInterface {
    id: string;
    question: string;
    correctAnswer: string;
}


export class QuizzessDataSource extends DataSource {
    getQuizzess = async (parent, args) => {
        const query = `SELECT * FROM c`;
        const results = await this.findManyByQuery(
          {
            query,
          },
        );
        return results.resources;
    };
}

Solution

  • I suspect you're not actually returning anything from your dataSources because js is interpreting the curly braces as the body of a function instead of an object (although it should then have returned an unexpected token error).

    Either:

    const dataSources = () => {
        return { quizzessApi: new QuizzessDataSource() };
    };
    

    or

    const dataSources = () => ({ quizzessApi: new QuizzessDataSource() });