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

Add named sub/child types to query/mutation/subcription results #4241

Closed
MrBlenny opened this issue Jun 16, 2020 · 1 comment
Closed

Add named sub/child types to query/mutation/subcription results #4241

MrBlenny opened this issue Jun 16, 2020 · 1 comment

Comments

@MrBlenny
Copy link

First, let me say - this is a fantastic library which has really improved by graphql experience. One issue I often have... I'll have a query/subscription or mutation which returns child entities. Eg)

subscription getParentWithChildren ($id: uuid!){
  parent_by_pk(id:$id) {
    id
    name
    children {
      id
      name
    }
  }
}

This will generate the following code:

export type Parent = {
  __typename?: 'parent';
 id: Scalars['uuid'];
 name: Scalars['String'];
 other: Scalars['String'];
};

export type Child = {
  __typename?: 'child';
 id: Scalars['uuid'];
 name: Scalars['String'];
 other: Scalars['String'];
};

export type GetParentWithChildrenSubscription = (
  { __typename?: 'subscription_root' }
  & { parent_by_pk?: Maybe<(
    { __typename?: 'parents' }
    & Pick<Parent, 'id' | 'name'>
    & { children: Array<(
      { __typename?: 'child' }
      & Pick<Child, 'id' | 'name'>
    )> }
  )> }
);

I'll then often have to use the type of the child somewhere, for example in a react component that just deals with rendering the child. I try and accomplish this by picking something from the subscription type, eg GetParentWithChildSubscription['parent_by_pk']['children'] or Requred<GetParentWithChildSubscription['parent_by_pk']>['children']. In some instances this works but in this particular case I get the following error:

Property 'children' does not exist on type '({ __typename?: "parents" | undefined; } & Pick<Parent, "id" | "name"> & { children: ({ __typename?: "child" | undefined; } & Pick<Child, "id" | "name">)[]; }) | null | undefined'.ts(2339)

Describe the solution you'd like
Would it make sense to export named types for these child properties. These would need to be namespaced by the query name and the path to the property. Eg)

export type GetParentWithChildrenSubscriptionParentByPkParent =  Pick<Parent, 'id' | 'name'>
export type GetParentWithChildrenSubscriptionParentByPkChild =  Pick<Child, 'id' | 'name'>

export type GetParentWithChildrenSubscription = (
  { __typename?: 'subscription_root' }
  & { parent_by_pk?: Maybe<(
    { __typename?: 'parents' }
    & GetParentWithChildrenSubscriptionParentByPkParent
    & { children: Array<(
      { __typename?: 'child' }
      & GetParentWithChildrenSubscriptionParentByPkChild
    )> }
  )> }
);
@dotansimha
Copy link
Owner

Hi @MrBlenny !
This was discussed a long ago here, please read this for tips and solutions: #2735 (comment)

Basically, this works in your example, but in large projects, those types are getting duplicated, and you get non-readable types (User_Something_Something_Something_1) and so on, and far more complex to maintain.

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

No branches or pull requests

2 participants