Type-graphql: Adding argument decorator breaks azure functions

Created on 6 Apr 2019  路  6Comments  路  Source: MichalLytek/type-graphql

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.

Duplicate Question

All 6 comments

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.

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.

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 馃敀

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.

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 馃槙

Set emitDecoratorMetadata: true in tsconfig.json

Was this page helpful?
0 / 5 - 0 ratings

Related issues

itsgracian picture itsgracian  路  3Comments

robertchung97 picture robertchung97  路  3Comments

oliversalzburg picture oliversalzburg  路  3Comments

Tybot204 picture Tybot204  路  3Comments

tafelito picture tafelito  路  3Comments