我有一个节点,express服务器使用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.raphql
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 });
这是做这件事的方式吗?
那么,我需要配置哪些typescript才能导入和构建架构
发布于 2018-07-26 04:55:59
AFAIK,有两种导入模式文件的方法,1)通过如上所述直接读取文件,或者2)通过将查询包装在导出的变量中。
// bookSchema.ts <- note the file extension is .ts instead of .graphql
export default `
type Book {
message: String
}
`
// anotherSchema.ts <- note the file extension is .ts instead of .graphql
export default `
type User {
name: String
}
`
// main.ts
import bookSchema from 'bookSchema';
import anotherSchema from 'anotherSchema';
const schema = makeExecutableSchema({ typeDefs: [
bookSchema,
anotherSchema,
] });
发布于 2020-10-06 19:48:56
你可以使用https://github.com/ardatan/graphql-import-node来解决这个问题,而不需要使用webpack。
使用yarn add graphql-import-node
或npm install --save graphql-import-node
安装,然后使用节点挂钩(如果使用ts- graphql-import-node/register
):
ts-node -r graphql-import-node/register index.ts
或者将其导入到文件顶部,如下所示:
import "graphql-import-node";
在我的例子中,我选择了后者,因为我已经在测试中使用了ts-node/register
和mocha -r
。
您可能还需要在tsconfig.json
中将"esModuleInterop": true
添加到compilerOptions中。
发布于 2019-04-18 08:26:17
这个答案解决了@leogoesger提出的问题。这是一种使用.graphql
文件创建模式的模块化方法,无需定义多个makeExecutableSchema
调用。
文件夹结构应该类似如下所示,才能正常工作:
src
- graphql
- schema.ts
- bar
- barResolver.ts
- schema.graphql
- foo
- fooResolver.ts
- schema.graphql
schema.graphql
包含您所有的类型定义。
在schema.ts
文件中,您可以创建合并模式,如下所示:
import { mergeSchemas, makeExecutableSchema } from "graphql-tools";
import { readdirSync, lstatSync, existsSync } from "fs";
import * as path from "path";
import { importSchema } from 'graphql-import'
import { GraphQLSchema } from 'graphql';
const schemas: GraphQLSchema[] = [];
const isDirectory = dirPath => existsSync(dirPath) && lstatSync(dirPath).isDirectory();
const getDirectories = source =>
readdirSync(source).map( name => path.join(source, name) ).filter(isDirectory)
const folders = getDirectories( path.resolve(__dirname, './') )
folders.forEach(folder => {
folder = folder.substr( folder.lastIndexOf("\\")+1 )
const {resolvers} = require(`./${folder}/${folder}Resolver`);
const typeDefs = importSchema( path.join(__dirname, `./${folder}/schema.graphql`) );
schemas.push(makeExecutableSchema({resolvers, typeDefs}))
});
const mergedSchemas = mergeSchemas({ schemas })
export default mergedSchemas;
其思想是获取与schema.ts
存在于同一级别的所有相对目录,然后遍历每个功能名称并导入相应的解析器和类型定义。接下来,我们使模式成为可执行的,并将其添加到我们的模式数组中。最后,我们使用mergeSchemas
将模式缝合在一起,从多个GraphQL创建一个API模式(有关更多详细信息,请参阅https://www.apollographql.com/docs/graphql-tools/schema-stitching )。
然后,您可以照常创建您的服务器
import schema from './graphql/schema';
const server = new GraphQLServer({schema: schema})
https://stackoverflow.com/questions/51525516
复制相似问题