-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Question: codebase full of TS Pick<> #1904
Comments
If you use fragments inside your queries that will generate matching types which you can use inside your code. |
Hi @elie222 , Doing As @lukasluecke said, fragments are a good solution for separating GraphQL operations and selection-sets, and we respect this separation by creating a separate type for fragments. We are thinking about putting the resolved type instead of Keeping open because we might implement the suggestion I mentioned above. |
Hi @dotansimha, We encounter a similar issue with passing the result of a Do you have any inputs regarding this? maybe a simple workaround meanwhile? |
@ozsay can you please share an example? I didn't understand how to reproduce it. We are working on an alternative for the |
@dotansimha schema
query
with this setup, the generated code looks like (with hooks): import gql from 'graphql-tag';
import * as ReactApolloHooks from 'react-apollo-hooks';
export type Maybe<T> = T | null;
export type MaybePromise<T> = Promise<T> | T;
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
Date: any;
};
export type User = {
__typename?: 'User';
id: Scalars['String'];
firstName: Scalars['String'];
lastName: Scalars['String'];
};
export type UsersQueryVariables = {};
export type UsersQuery = { __typename?: 'Query' } & {
users: Array<
{ __typename?: 'User' } & Pick<
User,
'id' | 'firstName'
>
>;
};
export const UsersDocument = gql`
query Users {
users {
id
firstName
}
}
`;
export function useUsersQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<UsersQueryVariables>) {
return ReactApolloHooks.useQuery<UsersQuery, UsersQueryVariables>(
UsersDocument,
baseOptions,
);
} and the code that raises problems: UsersList Component function UsersList() {
const { data: { users = [] } = {}, error, loading, refetch } = useUsersQuery(getUsers);
const renderItem = ({ item } : { item: User }) => <UserComponent user={item} />;
return (
<Container>
<FlatList
data={users}
renderItem={renderItem}
refreshing={loading}
onRefresh={refetch}
/>
</Container>
);
} User Component import React from 'react';
import { User } from './generated-code.tsx';
interface Props {
user: User;
}
export function UserComponent({ user } : Props) {
return ...
} The problem is obvious here. the users array that is returned from the query is of type |
You can use UsersQuery[“users”] for the flatlist as mentioned above. Or a
fragment and then use the fragment type.
Or you can have flatlist receive Partial<User> but this is less optimal.
…On Wed, 3 Jul 2019 at 14:47 Oz Sayag ***@***.***> wrote:
@dotansimha <https://github.com/dotansimha>
for example
schema
type Query {
users: [User!]!
}
type User {
id: String!
firstName: String!
lastName: String!
}
query
const getUsers = gql`
query Users {
users {
id
firstName
}
}
`;
with this setup, the generated code looks like (with hooks):
import gql from 'graphql-tag';import * as ReactApolloHooks from 'react-apollo-hooks';export type Maybe<T> = T | null;export type MaybePromise<T> = Promise<T> | T;/** All built-in and custom scalars, mapped to their actual values */export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
Date: any;
};
export type User = {
__typename?: 'User';
id: Scalars['String'];
firstName: Scalars['String'];
lastName: Scalars['String'];
};
export type UsersQueryVariables = {};
export type UsersQuery = { __typename?: 'Query' } & {
users: Array<
{ __typename?: 'User' } & Pick<
User,
'id' | 'firstName'
>
>;
};
export const UsersDocument = gql` query Users { users { id firstName } }`;
export function useUsersQuery(baseOptions?: ReactApolloHooks.QueryHookOptions<UsersQueryVariables>) {
return ReactApolloHooks.useQuery<UsersQuery, UsersQueryVariables>(
UsersDocument,
baseOptions,
);
}
and the code that raises problems:
UsersList Component
function UsersList() {
const { data: { users = [] } = {}, error, loading, refetch } = useUsersQuery(getUsers);
const renderItem = ({ item } : { item: User }) => <UserComponent user={item} />;
return (
<Container>
<FlatList
data={users}
renderItem={renderItem}
refreshing={loading}
onRefresh={refetch}
/>
</Container>
);
}
User Component
import React from 'react';
import { User } from './generated-code.tsx';
interface Props {
user: User;
}
export function UserComponent({ user } : Props) {
return ...
}
The problem is obvious here. the users array that is returned from the
query is of type Pick<User, 'id' | 'firstName' while the FlatList is
expecting User. Since the generated code doesn't export the pick type, i
can't use it to replace User type.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1904?email_source=notifications&email_token=AAXSQX2MFOID2VNHQGR3ZDDP5SGWBA5CNFSM4HOMRDKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODZEGAMY#issuecomment-508059699>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAXSQXZ2BTKWVKS7ADDSTB3P5SGWBANCNFSM4HOMRDKA>
.
|
@elie222 @ozsay I tried it in: #2107 The output is: export type MeQuery = {
__typename?: 'Query';
currentUser: Maybe<{ __typename?: 'User'; login: string; html_url: string }>;
entry: Maybe<{
__typename?: 'Entry';
id: number;
createdAt: number;
postedBy: { __typename?: 'User'; login: string; html_url: string };
}>;
}; What do you think? (I'll introduce a new config flag |
I'm with @dotansimha and @lukasluecke
This approach is recommended in Apollo and it close to the one that Relay is based off.
|
Actually, there are 2 issues here:
|
Available in 1.4.0 🎉 |
I'm generating TS types for operations using:
https://graphql-code-generator.com/docs/plugins/typescript-operations
The generated code produces a lot of types with
Pick
. For example:This is technically accurate, but a headache to fill the codebase with Pick when passing down properties.
What I find myself doing to fix this is:
policy as Policy
and assuming I have a fullPolicy
. The downsides are that it doesn't provide full type safety and it's annoying having to addas Policy
for each query.Another idea is to just be selective about the data being passed down in my components, but that doesn't sound fun either.
One last idea that just came to mind is to turn the Pick into a type, and the generator could be updated to do this automatically.
The text was updated successfully, but these errors were encountered: