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

Adding argument decorator breaks azure functions #303

Closed
meds opened this issue Apr 6, 2019 · 6 comments
Closed

Adding argument decorator breaks azure functions #303

meds opened this issue Apr 6, 2019 · 6 comments
Labels
Duplicate 🔑 This issue or pull request already exists Question ❔ Not future request, proposal or bug issue

Comments

@meds
Copy link

meds commented Apr 6, 2019

I'm using type-graphql with Azure Functions typescript, everything works except when I try to create input types, like so:

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

  @Field(type => [String])
  ingredients: string[]
}


@Resolver(of => Recipe)
class RecipeResolver {   
    @Query(returns => Recipe, { nullable: true })
    recipe(@Arg("title") title: string): Promise<Recipe | undefined> {
      return "abc"
    }
}

@arg("title") seems to be breaking azure functions, when it's in there I get the following error when I try to deploy and test the function locally:

[error] Worker was unable to load function graphql: 'TypeError: Cannot read property '0' of undefined'

I tracked the issue down to findType.js and much to my surprise wrapping reflectedType[parmaterIndex] in a try/catch it worked fine:

    if (metadataKey === "design:paramtypes") {
        try {
        metadataDesignType = reflectedType[parameterIndex];
        }
        catch(e) {
            console.log("#####################################################################");
            console.log(JSON.stringify(e));
        }
    }

I'm not suggesting this as a fix as I haven't fully tested it beyond the simple example given above, but thought it would be worth bringing up for discussion.

@MichalLytek MichalLytek added Bug 🐛 Something isn't working Need More Info 🤷‍♂️ Further information is requested labels Apr 6, 2019
@MichalLytek
Copy link
Owner

I'm not sure but it looks like Azure Functions is using babel-typescript which doesn't support argument decorators - see #55.

Catching the error will swallow the error and it will result in no args registering in TypeGraphQL.

Please try to investigate the decorators support in Azure Functions by creating own custom decorators with console.log to check if they are transpiled and evaluated properly. Then we can dig in to see the reflected metadata for parameters.

@meds
Copy link
Author

meds commented Apr 25, 2019

When I catch the error I don't notice any adverse effects, graphql still works perfectly - perhaps there's some quality of life features which are breaking that I'm unaware of?

I'll try to do what you asked however I think you're right and it's simply azure functions not supporting it.

Your comment on the 8th of Feb in that thread about calling it as a function seems like an acceptable work around, I'll try that as well to see if it helps.

Otherwise I'll probably move to a different platform (google firebase functions perhaps?) - the way I see it is type-graphql is definitely worth switching functions platforms for because it offers something very valuable.

@MichalLytek
Copy link
Owner

Based on #323, you should use babel-plugin-transform-typescript-metadata to have support for argument decorators (and base types reflection).

So closing this as a duplicate of #55 🔒

@MichalLytek MichalLytek added Duplicate 🔑 This issue or pull request already exists Question ❔ Not future request, proposal or bug issue and removed Bug 🐛 Something isn't working Need More Info 🤷‍♂️ Further information is requested labels Apr 28, 2019
@meds
Copy link
Author

meds commented Jun 1, 2019

I wasn't able to get this to work using the plugin you suggested because there's no webpack.config.json included, in the end the solution was to install https://www.npmjs.com/package/reflect-metadata

and enable emitDecoratorMetadata and experimentalDecorators in tsconfig, set target to es6 and add 'import reflect-metadata' to the top of tiles using decorators.

@MichalLytek
Copy link
Owner

and enable emitDecoratorMetadata and experimentalDecorators in tsconfig, set target to es6 and add 'import reflect-metadata' to the top of tiles using decorators.

That are the basic things described in installation section:
https://typegraphql.ml/docs/installation.html

Weird that it worked except argument decorator 😕

@shamilkhan
Copy link

Set emitDecoratorMetadata: true in tsconfig.json

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 Question ❔ Not future request, proposal or bug issue
Projects
None yet
Development

No branches or pull requests

3 participants