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

Can't use FindConditions with generics #7338

Closed
delucca opened this issue Feb 2, 2021 · 1 comment · Fixed by #7996
Closed

Can't use FindConditions with generics #7338

delucca opened this issue Feb 2, 2021 · 1 comment · Fixed by #7996
Assignees
Labels

Comments

@delucca
Copy link

delucca commented Feb 2, 2021

Issue Description

Expected Behavior

While coding abstract classes for my application, I would like to use generic types as their entities and be able to execute a query.

Actual Behavior

I've created an abstract class that is extended by my entity services. While doing so, I have some methods that can execute queries. Everything is working as expected, but my type system breaks as soon as I use any method that uses FindCondition as an argument. Here is a minimal example:

abstract class GraphQLEntityResolver<E extends DomainEntity, D> {
  
  (...)

  public async getUserPolicies(selector: FindConditions<E>, user: AuthzUser) {
    const actionSelectors: AuthzScopeGroup = {
      [ACTION.CREATE]: user.scopes[this.resource][ACTION.CREATE],
      [ACTION.READ]: user.scopes[this.resource][ACTION.READ],
      [ACTION.UPDATE]: user.scopes[this.resource][ACTION.UPDATE],
      [ACTION.DELETE]: user.scopes[this.resource][ACTION.DELETE],
    }

    const policies: ActionPolicies = mapValues(
      actionSelectors,
      async (constraint, action: ACTION): Promise<POLICY> => {
        if (!constraint) return POLICY.DENY
        const foundData = await this.getOneWithActionScopeConstraint(selector, user, action)

        return foundData ? POLICY.ALLOW : POLICY.DENY
      },
    )

    return policies
  }

  @ResolveField('policies', () => PolicyObject)
  protected async getEntityPolicies(
    @Parent() entity: EntityObject,
    @GraphQLUser() user: AuthzUser,
  ) {
    const selector: FindConditions<E> = { id: entity.id }

    return this.getUserPolicies(selector, user)
  }
}

At the getEntityPolicies method, the selector I'm building is giving me the following error:

src/app/graphql/resolver.ts:138:11 - error TS2322: Type '{ id: string; }' is not assignable to type 'FindConditions<E>'.

But, here is the DomainEntity entity that I'm using:

export abstract class DomainEntity {
  @PrimaryGeneratedColumn('uuid')
  public id: string

  @CreateDateColumn()
  public createdAt: Date
}

Also, if I change my class to:

abstract class GraphQLEntityResolver<D> {

And always use DomainEntity instead of E, it works as expected.

So, it seems that FindConditions<E> is not considering the keys of the DomainEntity abstract entity class. Since it has id but the selector is complaining, saying that I can't use ID as a selector.

@imnotjames
Copy link
Contributor

Looks like this was caused because by the jank in FindConditions types.

That was because Typescript 3.2 -> 3.6 had a problem with nested types. See #4470

However, it seems that this is no longer a problem with the latest versions ( > 3.6.3) so I'll be removing that workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants