meteor-graphql icon indicating copy to clipboard operation
meteor-graphql copied to clipboard

Typescript usage

Open asemoon opened this issue 8 years ago • 4 comments

I was wondering if you guys have tried this plugin with typescript? I want to import a .graphql in a typescript file, but it tries to load it as a module. My module resolution in tsconfig is set to node. So the following does not work for me: import * as schema from "../models/typeDefs.graphql"; However, If I use require like the following, it will work: const schema = require("../models/typeDefs.graphql"); I don't want to use require since it's discouraged by the TS community and my linting rules. Any idea how I can get this to work?

asemoon avatar Sep 07 '17 05:09 asemoon

Hi, I'm not familiar with module resolution in typescript, but why does the first statement not work?

What if you try this:

import schema from "../models/typeDefs.graphql";

What does it return? Or does typescript give you compile errors?

Cheers

jamiter avatar Sep 07 '17 10:09 jamiter

typescript does not recognize .graphql files by default. so I had to add the following in a definition file (.d.ts)

declare module "*.graphql" {
    const value: any;
    export = value;
}

asemoon avatar Sep 15 '17 19:09 asemoon

A bit late reply, but I understand that the issue was solved?

jamiter avatar Feb 03 '18 16:02 jamiter

I met similar issue with my setup(react+apollo+meteor+ts). What @asemoon mentioned is useful, we just had to let TS to pickup that definition file with the following setup in tsconfig.json -

"compilerOptions": {
    ...
    "typeRoots": ["node_modules/@types", "imports/@types"],
    ...
}
"files": ["imports/@types/graphql.d.ts"],
"include": ["**/*"],

lyslim avatar Jul 01 '20 06:07 lyslim