dotansimha / dotansimha/graphql-code-generator

`preset: client` doesn't detect documents in `.astro` files, but does watch the files properly

Open
#9,221 3 comments 5 reactions 0 assignees View on GitHub
stage/1-reproduction
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

@graphql-codegen/cli, @graphql-codegen/client-preset

### Describe the bug

Setting up the codegen to watch `documents: ['./**/*.astro']` results in the file getting watched correctly, but it doesn't seem like it's able to determine any GraphQL documents and type them properly.

For example, the following frontmatter of an Astro file will result in `postsQuery` being typed as `undefined`, but moving the query into a `.ts` file _will_ correctly type the query:

```
---
import Layout from '../layouts/Layout.astro';
import { graphql } from '../../gql';

const postsQuery = graphql(`
query PostsQuery {
posts {
nodes {
title
id
databaseId
}
}
}
`)
---
```

Changing the file does trigger the codegen to re-run while in watch mode, but the type of the query won't get generated.

### Your Example Website or App

Create a new astro app as described below

### Steps to Reproduce the Bug or Issue

1. Create a new astro app with `npm create astro@latest`
2. Create a `codegen.ts` file in the app root with these settings:
```
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
schema: "https://swapi-graphql.netlify.app/.netlify/functions/index",
documents: ['./**/*.astro', './**/*.ts'],
config: {
dedupeFragments: true,
},
generates: {
'./gql/': {
preset: 'client',
plugins: [],
config: {
dedupeFragments: true,
enumsAsTypes: true,
useTypeImports: true,
},
},
},
};

export default config;
```
3. Run `yarn graphql-codegen`
4. Create a new query in the `src/pages/index.astro` file:
```
---
import Layout from '../layouts/Layout.astro';
import { graphql } from '../../gql';

const allFilmsWithVariablesQueryDocument = graphql(`
query allFilmsWithVariablesQuery($first: Int!) {
allFilms(first: $first) {
edges {
node {
...FilmItem
}
}
}
}
`)
---
```
5. Re-run the generator

The `allFilmsWithVariablesQueryDocument` document will stay typed as `unknown`.

### Expected behavior

As a user, I would expect that the document is detected by the codegen and returns a typed document node. Instead, it stays as `unknown`.

### Screenshots or Videos

_No response_

### Platform

- OS: macOS
- NodeJS: 16.14.2
- `graphql` version: 16.6.0
- `@graphql-codegen/cli` version(s): 3.2.2
- `"@graphql-codegen/client-preset` version(s): 2.1.1

### Codegen Config File

_No response_

### Additional context

_No response_

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.