Graphql: Error: Abstract methods can only appear within an abstract class

Created on 27 Dec 2018  Â·  15Comments  Â·  Source: nestjs/graphql

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:

bug

Most helpful comment

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

All 15 comments

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

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[]>;
}

It actually should be fixed in 5.5.0+

@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?

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, 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.

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

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

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

I'm fixing it manually everytime by the moment.

@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.

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 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 thanks for the reply. My schema is changing pretty rapidly so I elected to switch everything over to interfaces which resolved the issue.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radarsu picture radarsu  Â·  3Comments

ghost picture ghost  Â·  5Comments

harm-less picture harm-less  Â·  4Comments

fenos picture fenos  Â·  5Comments

danil-z picture danil-z  Â·  4Comments