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

[FEATURE] suggestions for gqlFelds #596

Closed
draylegend opened this issue May 3, 2021 · 4 comments · Fixed by #698
Closed

[FEATURE] suggestions for gqlFelds #596

draylegend opened this issue May 3, 2021 · 4 comments · Fixed by #698

Comments

@draylegend
Copy link

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
      }
    }
  }
}
@satanTime
Copy link
Owner

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

@satanTime
Copy link
Owner

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

@satanTime
Copy link
Owner

satanTime commented Jun 4, 2021

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}',
        },
    });

@satanTime
Copy link
Owner

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.

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