Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type checking not working for resolver return values #290

Closed
elie222 opened this issue Mar 26, 2019 · 1 comment
Closed

Type checking not working for resolver return values #290

elie222 opened this issue Mar 26, 2019 · 1 comment
Labels
Duplicate 🔑 This issue or pull request already exists

Comments

@elie222
Copy link

elie222 commented Mar 26, 2019

Using the recipe example, the following should throw a TS error, but doesn't:

@ObjectType()
class Recipe {
  @Field(type => ID)
  id: string;

  @Field()
  title: string;
}

@Resolver(Recipe)
export class RecipeResolver {
  @Query(returns => Recipe)
  async recipe(@Arg("id") id: string) {
    return {}; // expected to throw a TS error as it doesn't have the `id` or `title` properties, but works fine!
  }
}

Running the following query in GraphQL Playground does throw an error as expected:

{
  recipe(id: "123") {
    id
    title
  }
}

Error:

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field Recipe.id.",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ],
      "path": [
        "recipe",
        "id"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "Error: Cannot return null for non-nullable field Recipe.id.",
          ]
        }
      }
    }
  ],
  "data": null
}
@MichalLytek
Copy link
Owner

MichalLytek commented Mar 27, 2019

The @Field(type => [Rate]) is only for providing type info when there's not enough TypeScript's reflection metadata. It doesn't perform the typechecking - the proposal is described in issue #221.

To have a type checking, you have to provide an explicit type annotation (e.g. : Promise<Recipe>):

  @Query(returns => Recipe)
  async recipe(@Arg("id") id: string): Promise<Recipe> {
    return {}; // throw a TS error
  }

@MichalLytek MichalLytek added the Duplicate 🔑 This issue or pull request already exists label Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate 🔑 This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants