graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Error: Abstract methods can only appear within an abstract class

Open biryukvy opened this issue 7 years ago • 15 comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

  1. Define parameters in property of the type, like this:
type Cat {
  ...
  food(brand: String): [Food]
}
  1. Restart your server to apply changes. This will add the abstract method to the class of the type in src/graphql.schema.ts:
export class Cat {
    ...
    abstract food(brand?: string): Food[] | Promise<Food[]>;
}
  1. Restart server (to apply any other changes, for example) and you will get the error:

TSError: ⨯ Unable to compile TypeScript: src/graphql.schema.ts(11,5): error TS1244: Abstract methods can only appear within an abstract class.

Expected behavior

Error must not be thrown on server start after src/graphql.schema.ts update

Minimal reproduction of the problem with instructions

https://github.com/biryukvy/nest-gql-abstract-method-issue (Changes, that have been added to reproduce the issue can be found in the commit: 87053d)

What is the motivation / use case for changing the behavior?

It is needed to pass parameters to subqueries

Environment


Nest version: 5.5.0

 
For Tooling issues:
- Node version: 8.14.1  
- Platform: Linux Ubuntu 18.04 

Others:

biryukvy avatar Dec 27 '18 15:12 biryukvy

What would be the desired output, I wonder? Would it be the following?

export class Cat {
   food(brand?: string): Food[] | Promise<Food[]> {
      throw new NotImplementedError();
   }
}

dhritzkiv avatar Jan 05 '19 02:01 dhritzkiv

I have the same problem

Given the following graphql type:

type Image {
  url(options: ImageOptions): String!
}

Te result of the code generation process is:

export class Image {
    abstract url(options?: ImageOptions): string | Promise<string>;
}

An abstrat method cannot be defined in a concrete class so, the class should be generated in the following way:

export abstract class Image {
    abstract url(options?: ImageOptions): string | Promise<string>;
}

What would be the desired output, I wonder? Would it be the following?

export class Cat {
   food(brand?: string): Food[] | Promise<Food[]> {
      throw new NotImplementedError();
   }
}

Well, the desired output would be:

export abstract class Cat {
   abstract food(brand?: string): Food[] | Promise<Food[]>;
}

Simonpedro avatar Jan 11 '19 19:01 Simonpedro

It actually should be fixed in 5.5.0+

kamilmysliwiec avatar Jan 15 '19 12:01 kamilmysliwiec

@kamilmysliwiec, For me, it is still reproducible, though I am using "@nestjs/graphql": "^5.5.1". Could you check the minimal reproducible example, that I've provided in the issue description?

biryukvy avatar Jan 21 '19 09:01 biryukvy

As far as I see, all methods are abstract (+ class is marked as abstract too) https://github.com/biryukvy/nest-gql-abstract-method-issue/blob/master/src/graphql.schema.ts

kamilmysliwiec avatar Jan 21 '19 09:01 kamilmysliwiec

@kamilmysliwiec, that is right - the repository contains the graphql schema of the app state, that was before I added the subquery. If you build the application, the non-abstract class "Cat" will obtain the abstract method "food". At this point, the server should be successfully started. But, after this, you will not be able to restart (rebuild) the application because of that abstract method.

biryukvy avatar Jan 21 '19 09:01 biryukvy

@kamilmysliwiec, the problem is not fixed, could you reopen the issue?

biryukvy avatar Jan 29 '19 09:01 biryukvy

@kamilmysliwiec, I have tried with nestjs v.5.5.0 and 5.6.0 - the issue is still present

vrozhkov avatar Feb 05 '19 06:02 vrozhkov

Any workaround for this at the moment? Experiencing the same issue.

triedal avatar Apr 05 '19 17:04 triedal

I'm fixing it manually everytime by the moment.

Simonpedro avatar Apr 05 '19 17:04 Simonpedro

@Simonpedro how are you doing that? My manual changes in graphql.schema.ts are overwritten every time I restart the server bc it is autogenerated.

triedal avatar Apr 05 '19 18:04 triedal

I think one way to work around this may be to change outputAs from class to interface. However, you would not be able to do any validation inside your DTO's.

triedal avatar Apr 05 '19 18:04 triedal

@triedal I just leave auto generation deactivated and turn it on after a change is made on queries or schema, an then fix the types manually. I'd be annoying to do this when the schema/queries are not stable but well 🤷

Simonpedro avatar Apr 05 '19 19:04 Simonpedro

@Simonpedro thanks for the reply. My schema is changing pretty rapidly so I elected to switch everything over to interfaces which resolved the issue.

triedal avatar Apr 05 '19 20:04 triedal

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Apr 25 '20 12:04 lock[bot]