typestack / typedi

Simple yet powerful dependency injection tool for JavaScript and TypeScript.
https://docs.typestack.community/typedi/v/develop/01-getting-started
MIT License
4.04k stars 168 forks source link

question: injected value is ContainerInstance instead of service #227

Closed herbertpimentel closed 3 years ago

herbertpimentel commented 3 years ago

I am trying following the simple typegraphql example, but using it as handle inside of nextjs app.

The problem: Everytime I am injecting the service it comes to me as "ContainerInstance" instead of the specific service

image

Here is a public repo to with this https://github.com/herbertpimentel/supersimples

So what I am possible doing wrong ?!

NoNameProvided commented 3 years ago

The container instance is always injected as the last extra parameter to allow the lib to work with JS where there are no decorators.

I see two possible reasons:

Both of these concepts have examples in the project readme: https://github.com/typestack/typedi

herbertpimentel commented 3 years ago

@NoNameProvided Thanks for the quick reply,

reflect-metadata is being called on the /page/api/graphql.ts this will be the very first file to be loaded on this api.

and the call for Container.get should be made by the typegraphql as specified in here https://typegraphql.com/docs/dependency-injection.html

The main difference I see from my code apollo-server-micro to the example apollo-server its uses a differente package for the server

NoNameProvided commented 3 years ago

This is an issue with how your app is built. It seems the meta-information about the parameter types are stripped by next build,

It should have meta data applied like this: Screenshot 2021-02-18 185625

but your build version looks like this:

Screenshot 2021-02-18 185804

I cannot help with this sorry. You are probably best of asking this on the Next repo. Your question should be about how to config the build process to preserve the emitted type information by Typescript.

herbertpimentel commented 3 years ago

Now you that you told me that metadata has been stiped off by nextjs. I could fix that.

Next.js does not use TSC (the original TypeScript compiler) and instead relies on Babel's @babel/preset-typescript package. Because of that, those tsconfig.json tweaks (to keep metadata on objects) do not have any effect.

Instead, I added custom Babel configuration in my Next.js project and included the equivalent options for Babel

{
  "presets": [
    [
      "next/babel",
      {
        "class-properties": {
          "loose": true
        }
      }
    ]
  ],
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
}

For future reference https://dev.to/unframework/getting-typeorm-to-work-with-next-js-and-typescript-1len

NoNameProvided commented 3 years ago

Glad it worked out. I am adding the docs label as this can be useful for many.

github-actions[bot] commented 3 years ago

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