Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript usage #15

Open
asemoon opened this issue Sep 7, 2017 · 4 comments
Open

Typescript usage #15

asemoon opened this issue Sep 7, 2017 · 4 comments

Comments

@asemoon
Copy link

asemoon commented Sep 7, 2017

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?

@jamiter
Copy link
Contributor

jamiter commented Sep 7, 2017

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

@asemoon
Copy link
Author

asemoon commented Sep 15, 2017

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;
}

@jamiter
Copy link
Contributor

jamiter commented Feb 3, 2018

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

@lyslim
Copy link

lyslim commented Jul 1, 2020

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": ["**/*"],

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

No branches or pull requests

3 participants