0

I'm trying to figure out if I need to add static to a private method within my class for an Apollo/GraphQl app. Thanks in advance!

export const example: Resolvers = {
  Query: {
    Example: async(_parent, {
      date
    }) => {
      return ExampleService.getExample(date);
    }
  },
};



class ExampleService {
  getExample(date: string) {
    const example = this.getExampleString();
    return example;
  }

  // HERE: do I need static, in general, or is this class already a singleton???
  private static getExampleString(): string {
    return 'Example!';
  }
}


// Side note: seems this is an anti-pattern according to the link provided below.
export default new ExampleService();

differences-between-creating-a-new-class-to-using-export-const

alphapilgrim
  • 3,761
  • 8
  • 29
  • 58
  • Is this all in one file? Are you really exporting both the resolver and the service instance from the same module? – Bergi Mar 10 '22 at 20:48
  • `ExampleService.getExample(date);` is a static method call. Not using `static` on `getExample` (presuming that `ExampleService` is a `class`, which it maybe shouldn't) won't work. Neither will calling a static method from a non-static method using `this`. – Bergi Mar 10 '22 at 20:50

0 Answers0