【发布时间】:2019-01-02 15:55:39
【问题描述】:
我有一个节点,使用 expressGraphql 表达服务器。我试图在.graphql 或.gql 文件中声明graphql 的类型定义,因为随着类型变大,阅读string 变得困难。
这是我所拥有的:
import testQuery from './test.graphql';
import routes from "./routes";
import { buildSchema } from "graphql";
const schema = buildSchema(testQuery);
// Root resolver
const root = {
message: () => "Hello World!",
};
app.use(
"/api/graphql",
expressGraphQL({
schema,
graphiql: true,
})
);
我的 graphql 文件。 //test.graphql
type Book {
message: String
}
我收到一个错误,因为 Typescript
找不到模块“./test.graphql”。
我见过有人这样做:
const { makeExecutableSchema } = require('graphql-tools');
const schemaFile = path.join(__dirname, 'schema.graphql');
const typeDefs = fs.readFileSync(schemaFile, 'utf8');
const schema = makeExecutableSchema({ typeDefs });
这是这样做的方式吗?
那么我需要如何配置打字稿才能导入和构建架构
【问题讨论】:
-
正如您所提到的,如果您不使用任何捆绑程序(如 webpack),那么将文件作为纯文本读取是最好的方法。这样,您还可以更轻松地 lint 和自动格式化 gql 文件
标签: typescript graphql