Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Types not generated for all files, ambiguous build error #89

Open
nathonius opened this issue May 2, 2020 · 5 comments
Open

Types not generated for all files, ambiguous build error #89

nathonius opened this issue May 2, 2020 · 5 comments

Comments

@nathonius
Copy link

It worked once, then decided to quit on me. I'm using gatsby-plugin-graphql-codegen with the default config, not gatsby-plugin-ts. It's very possible I've just done something wrong; I'm new to gatsby and graphql both, but this error message isn't very useful:

warn [gatsby-plugin-graphql-codegen] Cannot read property 'buildError' of undefined
info [gatsby-plugin-graphql-codegen] definition for queries of schema default-gatsby-schema has been updated at

Types are being generated, but missing for at least one file:

import React from 'react';
import { graphql } from 'gatsby';
import { QueryBlogPostBySlug } from '../../graphql-types'; // type error; module graphql-types has no exported member QueryBlogPostBySlug

interface BlogPostProps {
  data: QueryBlogPostBySlug;
}

const BlogPost: React.FC<BlogPostProps> = props => {
  // ...
};

export const query = graphql`
  query BlogPostBySlug($slug: String!) {
    site {
      siteMetadata {
        title
        author
      }
    }
    allButterPost(filter: { slug: { eq: $slug } }) {
      edges {
        node {
          id
          body
          seo_title
          date
          categories {
            name
          }
        }
      }
    }
  }
`;

export default BlogPost;

My query looks fine and runs fine, so I'm not sure what I'm missing. Repo is here.

@nathonius
Copy link
Author

Did some triangulation. Build seems to be choking on the fact that I have a file src/global.d.ts,

declare const __PATH_PREFIX__: string;
declare module 'typography-theme-moraga' {
  import { TypographyOptions } from 'typography';
  const Theme: TypographyOptions;
  export = Theme;
}
declare module 'typography-theme-judah' {
  import { TypographyOptions } from 'typography';
  const Theme: TypographyOptions;
  export = Theme;
}

As soon as I remove the file, the types are generated as expected.

@ricokahler
Copy link
Collaborator

I have a similar setup with type augments but my config is slightly different.

Can you try:

  1. moving src/global.d.ts to types/global.d.ts
  2. add this to your tsconfig.json
{
  // ...
  "compilerOptions": {
    // ...
    "typeRoots": [
      "node_modules/@types",
      "types"
    ],
  }
  // ...
}

Let me know if that does something.

@d4rekanguok
Copy link
Owner

Thanks for opening this issue @OfficerHalf, putting a global.d.ts in src dir is reasonable & shouldn't cause issue. Could you try @ricokahler's workaround for now? My apologies for the inconvenience.

It's possible that this is an issue with the upstream lib, I'll take a look.

@mshick
Copy link

mshick commented Jun 4, 2020

Same issue here. Quite a pain. Trying to move my types out of src per the suggestion seems to lead to a whole slew of other issues in my monorepo.

I spent a bit of time debugging and it's definitely an upstream lib — graphql-toolkit. The ability to include and exclude files by patterns is one option. If ambient types are universally the issue then .d.ts files could be excluded as a rule.

@mshick
Copy link

mshick commented Jun 4, 2020

Ah, I just found options.documentPaths. By excluding .d.ts files there I was able to solve my problem:

      documentPaths: [
        './src/**/!(*.d).{js,jsx,ts,tsx}',
        './.cache/fragments/*.js',
        './node_modules/gatsby-*/**/*.js'
      ]

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants