In my Typescript application, I have developed a Rest API using NestJS and interacting with a GraphQL server via Apollo Client.
Currently, my queries are hardcoded in the following manner:
const query = "query { example }";
this.apolloClient.query({
query: gql`
${query}
`,
});
I would like to store these queries in separate .graphql
files and use them as DocumentNode
.
example.graphql
query {
example
}
import example from "example.graphql";
this.apolloClient.query({
query: example,
});
I need assistance on how to achieve this.
I attempted creating a graphql.d.ts
file with the following code:
declare module "*.graphql" {
import { DocumentNode } from "graphql";
const value: DocumentNode;
export default value;
}
However, I keep encountering the error
Cannot find module "example.graphql" or its corresponding type declarations
.
import example from "example.graphql";
~~~~~~~~~~~~~~~~~