satanTime / ngrx-entity-relationship

ORM selectors for redux, @ngrx/store, @ngrx/entity and @ngrx/data. Ease of relationships with entities.

Home Page:https://ngrx-entity-relationship.sudo.eu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEATURE] suggestions for gqlFelds

draylegend opened this issue · comments

Issue

No typing for gqlFields

export const USER = 'user';

export interface UserEntity {
  id: string;
  firstName: string;
  lastName: string;
}

export interface State extends EntityState<UserEntity> {
  selectedId?: string;
  loaded?: boolean;
  error?: string;
}

export interface PartialState {
  readonly [USER]: State;
}

export const getFeature = createFeatureSelector<PartialState, State>(USER);

export const selectRootUser = rootEntitySelector(getFeature);

export const getUser = selectRootUser({ gqlFields: [''] }); // !Missing suggestions for gqlFields!

Possible sulotion

Provide an ENTITY

declare global {
    export interface SELECTOR_META<ENTITY = any> {
        flatKey?: string;
        gqlFields?: Array<keyof ENTITY>;
    }
}

adjust rootEntitySelector.ts by providing the ENTITY to SELECTOR_META => SELECTOR_META<ENTITY>.

This is how it might look after compilation:

import { FEATURE_SELECTOR, HANDLER_RELATED_ENTITY, HANDLER_ROOT_ENTITY, ID_TYPES, TRANSFORMER } from './types';
export declare function rootEntitySelector<STORE, ENTITY, TRANSFORMED>(featureSelector: FEATURE_SELECTOR<STORE, ENTITY>, transformer: TRANSFORMER<ENTITY, TRANSFORMED>, meta: SELECTOR_META<ENTITY>): (metaOrRelationship?: SELECTOR_META<ENTITY> | HANDLER_RELATED_ENTITY<STORE, ENTITY>, ...relationships: Array<HANDLER_RELATED_ENTITY<STORE, ENTITY>>) => HANDLER_ROOT_ENTITY<STORE, ENTITY, TRANSFORMED, ID_TYPES>;
export declare function rootEntitySelector<STORE, ENTITY, TRANSFORMED>(featureSelector: FEATURE_SELECTOR<STORE, ENTITY>, transformer: TRANSFORMER<ENTITY, TRANSFORMED>): (metaOrRelationship?: SELECTOR_META<ENTITY> | HANDLER_RELATED_ENTITY<STORE, ENTITY>, ...relationships: Array<HANDLER_RELATED_ENTITY<STORE, ENTITY>>) => HANDLER_ROOT_ENTITY<STORE, ENTITY, TRANSFORMED, ID_TYPES>;
export declare function rootEntitySelector<STORE, ENTITY>(featureSelector: FEATURE_SELECTOR<STORE, ENTITY>, meta: SELECTOR_META<ENTITY>): (metaOrRelationship?: SELECTOR_META<ENTITY> | HANDLER_RELATED_ENTITY<STORE, ENTITY>, ...relationships: Array<HANDLER_RELATED_ENTITY<STORE, ENTITY>>) => HANDLER_ROOT_ENTITY<STORE, ENTITY, ENTITY, ID_TYPES>;
export declare function rootEntitySelector<STORE, ENTITY>(featureSelector: FEATURE_SELECTOR<STORE, ENTITY>): (metaOrRelationship?: SELECTOR_META<ENTITY> | HANDLER_RELATED_ENTITY<STORE, ENTITY>, ...relationships: Array<HANDLER_RELATED_ENTITY<STORE, ENTITY>>) => HANDLER_ROOT_ENTITY<STORE, ENTITY, ENTITY, ID_TYPES>;

Profit and a happy developer:

image

We don't need to guess the entity props any more.

Use all entity props without providing'em all in gqlFields: ['id', '...']

Is it possible to make the gqlFields little more dynamic? For example explicit with gqlFields: 'all' or implicit if gqlFields is not provided, all the entity props are used to generate a graphql query/mutation/subscription.

The only question is, how to handle nested props like:

interface User {
  id: string;
  firstName: string;
  profile: Settings;
}

interface Settings {
  theme: 'light' | 'dark';
  permissions: Permission[];
  // ...
}

interface Permission {
  id: string;
  name: string;
  value: boolean;

Maybe it'd be even cooler with this approach like in prisma

export const getUser = selectRootUser({
  gqlFields: { id: true, firstName: true, profile: { permissions: { value: true } },
});

would generate following graphql query

{
  user {
    id
    firstName
    profile {
      permissions {
        value
      }
    }
  }
}

Aha, a good case. Thanks! I'll try to provide a fix for it next days, but might take 1-2 weeks.

I'm very sorry. It took a while. Now I'm back and working on the issue.

Hi @vladimirdrayling,

could you verify that the change does what you expect?

ngrx-entity-relationship.zip

    const user = rootEntitySelector(userFeature, {
        gqlFields: {
            id: '',
            firstName: '',
            lastName: '',
            permissions: '{key level}',
        },
    });

v1.6.0 has been released and contains a fix for the issue. Feel free to reopen the issue or to submit a new one if you meet any problems.