dotansimha / dotansimha/graphql-code-generator-community

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

Open
#132 8 comments 0 reactions 1 assignee Claimed by @eddeee888 View on GitHub
help wanted
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

It seems `import-types` preset adds "Types." prefix to all types, regardless if they actualy need to be imported.

Codegen config:

```yaml
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.:

```TypeScript
import * as Types from '@foo/graphql-schema';

// ...

// `SocialMediaLinks` is imported from `Types`, good
export type GetFooterDataQuery = { __typename?: 'Query' } & {
socialMediaLinks?: Types.Maybe<
Array>>
>;
};

// ...

// oh no, `GetFooterDataQuery` doesn't exist on `Types`!
export function useGetFooterDataQuery(
baseOptions?: Apollo.QueryHookOptions,
) {
const options = { ...defaultOptions, ...baseOptions };
return Apollo.useQuery(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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.