dotansimha/graphql-code-generator-community

[import-types-preset] Types should be imported only when the are external to the generated file

Open

#132 opened on 2021年6月23日

GitHub で見る
 (8 comments) (0 reactions) (1 assignee)TypeScript (195 forks)github user discovery
help wanted

Repository metrics

Stars
 (136 stars)
PR merge metrics
 (PR metrics pending)

説明

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?

コントリビューターガイド