cometkim / gatsby-plugin-typegen

Let's give developers using GatsbyJS better DX with extreme type-safety
https://www.gatsbyjs.org/packages/gatsby-plugin-typegen/
MIT License
204 stars 25 forks source link

Query type generation fails sometimes #162

Closed KaiseiSato closed 2 years ago

KaiseiSato commented 3 years ago

Overview

When running gatsby build, type generation sometimes fails. The output file is generated. (e.g., gatsby-types.d.ts) I am using the Headless CMS service. The data type definitions are always generated successfully, but the query type generation fails sometimes. Most query types are generated correctly. But some types are sometimes completed without being generated. When it fails, I have confirmed that the processing order of the program is abnormal.

Source for verification

gatsby-node.ts

...
(line 61)
try {
    const code = await readFile(componentPath);
    console.log('debug 1');
    const extractedSDL = await gqlPluckFromCodeString(
        componentPath,
        code,
        GRAPHQL_TAG_PLUCK_OPTIONS,
    );
    console.log('debug 2');
    if (extractedSDL) {
        console.log('debug 3');
        const document = parseGraphQLSDL(componentPath, extractedSDL, { noLocation: true });
        console.log('debug 4');
        trackedSource.set(componentPath, document);
        console.log('debug 5');
    }
} catch (error) {
    reporter.error(`[typegen] Fail to extract GraphQL documents from ${componentPath}`, error);
}

...

(line 95)
unsubscribeQueryExtraction();
console.log('END');

...

Result when completed correctly

...
debug 1
debug 2
debug 3
debug 4
debug 5
debug 1
debug 2
debug 3
debug 4
debug 5
END

...

Result when not completed correctly

debug 1
debug 2
debug 3
debug 4
debug 5
END
debug 1
debug 2
debug 3
debug 4
debug 5
cometkim commented 3 years ago

Thanks @KaiseiSato,

just to confirm if this is same issue with #160 did you enable PARRAEL_SOURCING flag?

KaiseiSato commented 3 years ago

@cometkim I have not changed the PARALLEL_SOURCING flag. So it's not enabled.

j-yamamoto commented 3 years ago

@cometkim Thank you for recognizing this as an issue. I don't mean to push you, but is it gonna be fixed anytime soon? If not, could you share us workarounds to avoid this issue?

cometkim commented 3 years ago

@j-yamamoto I'm not gonna fix this issue in v2 but in v3. Watch #138 for the progress.

v3 fix this issue by introducing a scheduler that relies on actual schema/definitions change instead of the order of lifecycle API.

I recommend that to commit and review the generated type definitions together for better DX.

cometkim commented 2 years ago

I recommend that to commit and review the generated type definitions together for better DX.

I admit it wasn't a good guide. Schema output was not stable in v2.

I fixed this (and also the schema output is now stable!) in v3 and it is available in the current RC version.

cometkim commented 2 years ago

Please feel free to report another issue If this is reproduced after v3