inversify / InversifyJS

A powerful and lightweight inversion of control container for JavaScript & Node.js apps powered by TypeScript.
http://inversify.io/
MIT License
11.21k stars 715 forks source link

@inject not working #1512

Closed dstoyanoff closed 1 year ago

dstoyanoff commented 1 year ago

I have turned around every corner of the internet about this issue, but couldn't find any solution.

I have configured Inversify in a project and everything seems to be working fine, besides trying to use @inject in a constructor or in a field.

When I resolve the dependencies using the container directly, it all works, which means the registration was successful, but the injection isn't.

Expected Behavior

@inject should work or throw an error

Current Behavior

The @inject decorator doesn't work and there are no errors in the console.

Context

I am using NextJS () and the flow is triggered from a GraphQL API route.

Your Environment

notaphplover commented 1 year ago

@dstoyanoff can you provide a minimal example? Otherwise it's going to be close to impossible to reproduce your issue.

dstoyanoff commented 1 year ago

I will try to reproduce it in a minimal environment and post a link here, thanks.

dstoyanoff commented 1 year ago

https://github.com/dstoyanoff/inversify-inject-issue

I've created the minimal reproduction. Run with yarn dev. I've exposed 3 different queries on the GraphQL endpoint (api/graphql) to showcase the different options:

// uses constructor injection (doesn't work)

query test {
    constructorTest
}

// uses field injection (doesn't work)

query test {
    fieldTest
}

// uses the container directly (works)

query test {
    containerGetTest
}

Let me know if anything else is needed

dstoyanoff commented 1 year ago

I actually found the problem. I had to instruct type-graphql about the use of the container and register my resolvers. Pushed a fix in the repo for reference