Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
fix: fixed infite looping in class constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Jul 8, 2021
1 parent 7711261 commit c07f96a
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 191 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
{
"extends": ["@sapphire"]
"extends": ["@sapphire"],
"overrides": [
{
"files": "src/services/*.ts",
"rules": {
"@typescript-eslint/no-extraneous-class": "off"
}
}
]
}
18 changes: 6 additions & 12 deletions src/resolvers/FilmResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ import { Args, Query, Resolver } from 'type-graphql';

@Resolver(Film)
export default class FilmResolver {
private filmService: FilmService;

public constructor() {
this.filmService = new FilmService();
}

@Query(() => Film, {
description: 'Gets details on a Star Wars film, using the episode number of that film'
})
public getFilm(@Args() args: FilmArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Film>): Film {
const filmData = this.filmService.getByEpisodeNumber(args);
const filmData = FilmService.getByEpisodeNumber(args);

if (!filmData) {
throw new Error(`No film found for ${args.film}`);
}

const graphqlObject = this.filmService.mapFilmDataToFilmGraphQL(filmData, requestedFields);
const graphqlObject = FilmService.mapFilmDataToFilmGraphQL(filmData, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for film: ${args.film}`);
Expand All @@ -43,24 +37,24 @@ export default class FilmResolver {
public getFuzzyFilm(@Args() args: FuzzyFilmArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Film>): Film[] {
const filmEpisodeId = Number(args.film);

let data = filmEpisodeId ? this.filmService.getByEpisodeNumber({ film: filmEpisodeId }) : undefined;
let data = filmEpisodeId ? FilmService.getByEpisodeNumber({ film: filmEpisodeId }) : undefined;

if (!data) {
const fuzzyEntry = this.filmService.findByFuzzy(args);
const fuzzyEntry = FilmService.findByFuzzy(args);

if (fuzzyEntry === undefined || !fuzzyEntry.length) {
throw new Error(`Failed to get data for film: ${args.film}`);
}

// TODO: Actually return multiple results by looping over this
data = this.filmService.getByEpisodeNumber({ film: fuzzyEntry[0].item.episodeId });
data = FilmService.getByEpisodeNumber({ film: fuzzyEntry[0].item.episodeId });

if (!data) {
throw new Error(`No film found for: ${args.film}`);
}
}

const graphqlObject = this.filmService.mapFilmDataToFilmGraphQL(data, requestedFields);
const graphqlObject = FilmService.mapFilmDataToFilmGraphQL(data, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for film: ${args.film}`);
Expand Down
18 changes: 6 additions & 12 deletions src/resolvers/PersonResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ import { Args, Query, Resolver } from 'type-graphql';

@Resolver(Person)
export default class PersonResolver {
private personService: PersonService;

public constructor() {
this.personService = new PersonService();
}

@Query(() => Person, {
description: 'Gets details on a Star Wars person, using the name of that person'
})
public getPerson(@Args() args: PersonArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Person>): Person {
const personData = this.personService.getByPersonName(args);
const personData = PersonService.getByPersonName(args);

if (!personData) {
throw new Error(`No person found for ${args.person}`);
}

const graphqlObject = this.personService.mapPersonDataToPersonGraphQL(personData, requestedFields);
const graphqlObject = PersonService.mapPersonDataToPersonGraphQL(personData, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for person: ${args.person}`);
Expand All @@ -41,24 +35,24 @@ export default class PersonResolver {
].join('\n')
})
public getFuzzyPerson(@Args() args: FuzzyPersonArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Person>): Person[] {
let data = this.personService.getByPersonName(args);
let data = PersonService.getByPersonName(args);

if (!data) {
const fuzzyEntry = this.personService.findByFuzzy(args);
const fuzzyEntry = PersonService.findByFuzzy(args);

if (fuzzyEntry === undefined || !fuzzyEntry.length) {
throw new Error(`Failed to get data for person: ${args.person}`);
}

// TODO: Actually return multiple results by looping over this
data = this.personService.getByPersonName({ person: fuzzyEntry[0].item.name });
data = PersonService.getByPersonName({ person: fuzzyEntry[0].item.name });

if (!data) {
throw new Error(`No person found for: ${args.person}`);
}
}

const graphqlObject = this.personService.mapPersonDataToPersonGraphQL(data, requestedFields);
const graphqlObject = PersonService.mapPersonDataToPersonGraphQL(data, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for person: ${args.person}`);
Expand Down
18 changes: 6 additions & 12 deletions src/resolvers/PlanetResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ import { Args, Query, Resolver } from 'type-graphql';

@Resolver(Planet)
export default class PlanetResolver {
private planetService: PlanetService;

public constructor() {
this.planetService = new PlanetService();
}

@Query(() => Planet, {
description: 'Gets details on a Star Wars planet, using the name of that planet'
})
public getPlanet(@Args() args: PlanetArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Planet>): Planet {
const planetData = this.planetService.getByPlanetName(args);
const planetData = PlanetService.getByPlanetName(args);

if (!planetData) {
throw new Error(`No planet found for ${args.planet}`);
}

const graphqlObject = this.planetService.mapPlanetDataToPlanetGraphQL(planetData, requestedFields);
const graphqlObject = PlanetService.mapPlanetDataToPlanetGraphQL(planetData, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for planet: ${args.planet}`);
Expand All @@ -41,24 +35,24 @@ export default class PlanetResolver {
].join('\n')
})
public getFuzzyPlanet(@Args() args: FuzzyPlanetArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Planet>): Planet[] {
let data = this.planetService.getByPlanetName(args);
let data = PlanetService.getByPlanetName(args);

if (!data) {
const fuzzyEntry = this.planetService.findByFuzzy(args);
const fuzzyEntry = PlanetService.findByFuzzy(args);

if (fuzzyEntry === undefined || !fuzzyEntry.length) {
throw new Error(`Failed to get data for planet: ${args.planet}`);
}

// TODO: Actually return multiple results by looping over this
data = this.planetService.getByPlanetName({ planet: fuzzyEntry[0].item.name });
data = PlanetService.getByPlanetName({ planet: fuzzyEntry[0].item.name });

if (!data) {
throw new Error(`No planet found for: ${args.planet}`);
}
}

const graphqlObject = this.planetService.mapPlanetDataToPlanetGraphQL(data, requestedFields);
const graphqlObject = PlanetService.mapPlanetDataToPlanetGraphQL(data, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for planet: ${args.planet}`);
Expand Down
18 changes: 6 additions & 12 deletions src/resolvers/SpeciesResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ import { Args, Query, Resolver } from 'type-graphql';

@Resolver(Species)
export default class SpeciesResolver {
private speciesService: SpeciesService;

public constructor() {
this.speciesService = new SpeciesService();
}

@Query(() => Species, {
description: 'Gets details on a Star Wars species, using the name of that species'
})
public getSpecies(@Args() args: SpeciesArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Species>): Species {
const speciesData = this.speciesService.getBySpeciesName(args);
const speciesData = SpeciesService.getBySpeciesName(args);

if (!speciesData) {
throw new Error(`No species found for ${args.species}`);
}

const graphqlObject = this.speciesService.mapSpeciesDataToSpeciesGraphQL(speciesData, requestedFields);
const graphqlObject = SpeciesService.mapSpeciesDataToSpeciesGraphQL(speciesData, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for species: ${args.species}`);
Expand All @@ -41,24 +35,24 @@ export default class SpeciesResolver {
].join('\n')
})
public getFuzzySpecies(@Args() args: FuzzySpeciesArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Species>): Species[] {
let data = this.speciesService.getBySpeciesName(args);
let data = SpeciesService.getBySpeciesName(args);

if (!data) {
const fuzzyEntry = this.speciesService.findByFuzzy(args);
const fuzzyEntry = SpeciesService.findByFuzzy(args);

if (fuzzyEntry === undefined || !fuzzyEntry.length) {
throw new Error(`Failed to get data for species: ${args.species}`);
}

// TODO: Actually return multiple results by looping over this
data = this.speciesService.getBySpeciesName({ species: fuzzyEntry[0].item.name });
data = SpeciesService.getBySpeciesName({ species: fuzzyEntry[0].item.name });

if (!data) {
throw new Error(`No species found for: ${args.species}`);
}
}

const graphqlObject = this.speciesService.mapSpeciesDataToSpeciesGraphQL(data, requestedFields);
const graphqlObject = SpeciesService.mapSpeciesDataToSpeciesGraphQL(data, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for species: ${args.species}`);
Expand Down
18 changes: 6 additions & 12 deletions src/resolvers/StarshipResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ import { Args, Query, Resolver } from 'type-graphql';

@Resolver(Starship)
export default class StarshipResolver {
private starshipService: StarshipService;

public constructor() {
this.starshipService = new StarshipService();
}

@Query(() => Starship, {
description: 'Gets details on a Star Wars starship, using the name of that starship'
})
public getStarship(@Args() args: StarshipArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Starship>): Starship {
const starshipData = this.starshipService.getByStarshipName(args);
const starshipData = StarshipService.getByStarshipName(args);

if (!starshipData) {
throw new Error(`No starship found for ${args.starship}`);
}

const graphqlObject = this.starshipService.mapStarshipDataToStarshipGraphQL(starshipData, requestedFields);
const graphqlObject = StarshipService.mapStarshipDataToStarshipGraphQL(starshipData, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for starship: ${args.starship}`);
Expand All @@ -41,24 +35,24 @@ export default class StarshipResolver {
].join('\n')
})
public getFuzzyStarship(@Args() args: FuzzyStarshipArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Starship>): Starship[] {
let data = this.starshipService.getByStarshipName(args);
let data = StarshipService.getByStarshipName(args);

if (!data) {
const fuzzyEntry = this.starshipService.findByFuzzy(args);
const fuzzyEntry = StarshipService.findByFuzzy(args);

if (fuzzyEntry === undefined || !fuzzyEntry.length) {
throw new Error(`Failed to get data for starship: ${args.starship}`);
}

// TODO: Actually return multiple results by looping over this
data = this.starshipService.getByStarshipName({ starship: fuzzyEntry[0].item.name });
data = StarshipService.getByStarshipName({ starship: fuzzyEntry[0].item.name });

if (!data) {
throw new Error(`No starship found for: ${args.starship}`);
}
}

const graphqlObject = this.starshipService.mapStarshipDataToStarshipGraphQL(data, requestedFields);
const graphqlObject = StarshipService.mapStarshipDataToStarshipGraphQL(data, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for starship: ${args.starship}`);
Expand Down
18 changes: 6 additions & 12 deletions src/resolvers/VehicleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ import { Args, Query, Resolver } from 'type-graphql';

@Resolver(Vehicle)
export default class VehicleResolver {
private vehicleService: VehicleService;

public constructor() {
this.vehicleService = new VehicleService();
}

@Query(() => Vehicle, {
description: 'Gets details on a Star Wars vehicle, using the name of that vehicle'
})
public getVehicle(@Args() args: VehicleArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Vehicle>): Vehicle {
const vehicleData = this.vehicleService.getByVehicleName(args);
const vehicleData = VehicleService.getByVehicleName(args);

if (!vehicleData) {
throw new Error(`No vehicle found for ${args.vehicle}`);
}

const graphqlObject = this.vehicleService.mapVehicleDataToVehicleGraphQL(vehicleData, requestedFields);
const graphqlObject = VehicleService.mapVehicleDataToVehicleGraphQL(vehicleData, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for vehicle: ${args.vehicle}`);
Expand All @@ -41,24 +35,24 @@ export default class VehicleResolver {
].join('\n')
})
public getFuzzyVehicle(@Args() args: FuzzyVehicleArgs, @getRequestedFields() requestedFields: GraphQLSet<keyof Vehicle>): Vehicle[] {
let data = this.vehicleService.getByVehicleName(args);
let data = VehicleService.getByVehicleName(args);

if (!data) {
const fuzzyEntry = this.vehicleService.findByFuzzy(args);
const fuzzyEntry = VehicleService.findByFuzzy(args);

if (fuzzyEntry === undefined || !fuzzyEntry.length) {
throw new Error(`Failed to get data for vehicle: ${args.vehicle}`);
}

// TODO: Actually return multiple results by looping over this
data = this.vehicleService.getByVehicleName({ vehicle: fuzzyEntry[0].item.name });
data = VehicleService.getByVehicleName({ vehicle: fuzzyEntry[0].item.name });

if (!data) {
throw new Error(`No vehicle found for: ${args.vehicle}`);
}
}

const graphqlObject = this.vehicleService.mapVehicleDataToVehicleGraphQL(data, requestedFields);
const graphqlObject = VehicleService.mapVehicleDataToVehicleGraphQL(data, requestedFields);

if (!graphqlObject) {
throw new Error(`Failed to get data for vehicle: ${args.vehicle}`);
Expand Down
Loading

0 comments on commit c07f96a

Please sign in to comment.