[import-types-preset] Types should be imported only when the are external to the generated file
#132 创建于 2021年6月23日
仓库指标
- Star
- (136 star)
- PR 合并指标
- (PR 指标待抓取)
描述
It seems import-types preset adds "Types." prefix to all types, regardless if they actualy need to be imported.
Codegen config:
generates:
src/api/index.ts:
schema: './node_modules/@foo/graphql-schema/schema.graphql'
documents: './queries.graphql'
preset: import-types
presetConfig:
typesPath: '@foo/graphql-schema'
plugins:
- typescript-operations
- typescript-react-apollo
config:
withHooks: true
Types for the schema (i.e. those generated by typescript plugin) and the schema itself are in package @foo/graphql-schema. Code generated by typescript-operations is ok, it contains typed GraphQL operations specified in queries.graphql and uses types imported from @foo/graphql-schema. But the code generated by typescript-react-apollo also tries to import stuff - but it shouldn't, as types for queries are defined right next to it, e.g.:
import * as Types from '@foo/graphql-schema';
// ...
// `SocialMediaLinks` is imported from `Types`, good
export type GetFooterDataQuery = { __typename?: 'Query' } & {
socialMediaLinks?: Types.Maybe<
Array<Types.Maybe<{ __typename?: 'SocialMediaLinks' } & Pick<Types.SocialMediaLinks, 'title' | 'type' | 'url'>>>
>;
};
// ...
// oh no, `GetFooterDataQuery` doesn't exist on `Types`!
export function useGetFooterDataQuery(
baseOptions?: Apollo.QueryHookOptions<Types.GetFooterDataQuery, Types.GetFooterDataQueryVariables>,
) {
const options = { ...defaultOptions, ...baseOptions };
return Apollo.useQuery<Types.GetFooterDataQuery, Types.GetFooterDataQueryVariables>(GetFooterDataDocument, options);
}
For now I think I solved it by splitting the generation in two steps, first generate types for operations, and then generate Apollo hooks using import-plugin once again, but importing the types generated in the first step - I assume that it's okay, because typescript-react-apollo doesn't actually need the schema types?