babel-plugin-import-graphql icon indicating copy to clipboard operation
babel-plugin-import-graphql copied to clipboard

src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations

Open Darfion opened this issue 3 years ago • 2 comments

hi I want to import a graphql file. but for some reason i am getting error

\node_modules\ts-node\src\index.ts:750
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations.

12 import typeDefs from "./schema.graphql";
                        ~~~~~~~~~~~~~~~~~~
 

how do i do the import

import typeDefs from "./schema.graphql";

graphql.d.ts

declare module "*.graphql" {
  import { DocumentNode } from "graphql";

  const value: DocumentNode;
  export = value;
}

.babelrc

{
  "plugins": ["import-graphql"]
}

full code can be viewed here

Darfion avatar Nov 09 '21 04:11 Darfion

this works for me:

const importGraphql = ['import-graphql', { emitDeclarations: true }];
const plugins = [importGraphql, ...etc];

webjay avatar Oct 25 '22 12:10 webjay

@webjay's comment worked, but the typings for DocumentNode for apollo vs what this outputs isn't working.

First it throws this typing issue:

 Types of property 'kind' are incompatible.
      Type '"Document"' is not assignable to type 'Kind.DOCUMENT'

And if you fix that it says loc is not typed correctly:

Types of property 'loc' are incompatible.
      Type '...' is missing the following properties from type 'Location': startToken, endToken, toJSON, [Symbol.toStringTag]

I've tried setting up my graphql.d.ts to use both the graphql & @apollo/client verions but neither satisfies the call to useQuery.

alburdette619 avatar Dec 18 '23 19:12 alburdette619