首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在没有webpack参与的typescript节点项目中使用.graphql

如何在没有webpack参与的typescript节点项目中使用.graphql
EN

Stack Overflow用户
提问于 2018-07-26 02:29:27
回答 3查看 6.7K关注 0票数 9

我有一个节点,express服务器使用expressGraphql。我尝试在.graphql.gql文件中声明graphql的类型定义,因为随着类型变得越来越大,读取string变得困难。

下面是我所拥有的

代码语言:javascript
复制
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

代码语言:javascript
复制
type Book {
    message: String
}

我收到一个错误,因为Typescript

找不到模块“./test.graphql”。

我见过很多人这样做:

代码语言:javascript
复制
const { makeExecutableSchema } = require('graphql-tools');

const schemaFile = path.join(__dirname, 'schema.graphql');
const typeDefs = fs.readFileSync(schemaFile, 'utf8');

const schema = makeExecutableSchema({ typeDefs });

这是做这件事的方式吗?

那么,我需要配置哪些typescript才能导入和构建架构

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-26 04:55:59

AFAIK,有两种导入模式文件的方法,1)通过如上所述直接读取文件,或者2)通过将查询包装在导出的变量中。

代码语言:javascript
复制
// 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,
] });
票数 4
EN

Stack Overflow用户

发布于 2020-10-06 19:48:56

你可以使用https://github.com/ardatan/graphql-import-node来解决这个问题,而不需要使用webpack。

使用yarn add graphql-import-nodenpm install --save graphql-import-node安装,然后使用节点挂钩(如果使用ts- graphql-import-node/register ):

ts-node -r graphql-import-node/register index.ts

或者将其导入到文件顶部,如下所示:

代码语言:javascript
复制
import "graphql-import-node";

在我的例子中,我选择了后者,因为我已经在测试中使用了ts-node/registermocha -r

您可能还需要在tsconfig.json中将"esModuleInterop": true添加到compilerOptions中。

票数 4
EN

Stack Overflow用户

发布于 2019-04-18 08:26:17

这个答案解决了@leogoesger提出的问题。这是一种使用.graphql文件创建模式的模块化方法,无需定义多个makeExecutableSchema调用。

文件夹结构应该类似如下所示,才能正常工作:

代码语言:javascript
复制
    src
    - graphql
     - schema.ts
     - bar
      - barResolver.ts
      - schema.graphql
     - foo
      - fooResolver.ts
      - schema.graphql

schema.graphql包含您所有的类型定义。

schema.ts文件中,您可以创建合并模式,如下所示:

代码语言:javascript
复制
    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 )。

然后,您可以照常创建您的服务器

代码语言:javascript
复制
import schema from './graphql/schema';
const server = new GraphQLServer({schema: schema})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51525516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档