首页 > 解决方案 > 在单独的文件中制作 typedef 以导入

问题描述

我正在使用 GraphQL、NodeJS 开发一个项目,并且我正在尝试创建一个名为的新文件typedef.ts,我想将其导入我的文件中,schema.ts但由于某种原因我无法使其工作。我得到的错误如下:

Error: typeDefs must be a string, array or schema AST, got object 

到目前为止,我在 schema.ts 中尝试了以下更改:

import { makeExecutableSchema } from 'graphql-tools';
import { TypeDefs } from './typedefs/typedefs';
import { resolvers } from '../graphql/resolvers';

const typeDefs: Array<string> = TypeDefs; // <- Added this

export const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
    logger: { log: e => console.log(e) },
});

typedef.ts:

import { types } from '../../graphql/types';
import { Types } from '../types/types';

const schemaDefinition = `
    schema {
        query         : Query
        mutation      : Mutation
    }
`;

    export const TypeDefs = {
        schemaDefinition,
        // Card Type Def
        cardDef: [Types.cardTypes.query, Types.cardTypes.mutation],

        // User Type Def
        userDef: [Types.userTypes.query, Types.userTypes.mutation],

        ...types,
    };

请向我解释如何解决此问题,因为我找不到解决方案。

标签: node.jsgraphql

解决方案


我不明白我的问题的减号,因为我正在学习这些技术,这是一个合法的猜测。如果我弄错了,我会很高兴得到解释,这样我才能理解。

顺便说一句,我解决了我的问题,如下所示:

架构.ts

import { makeExecutableSchema } from 'graphql-tools';
import TypeDefs from './typedefs/typedefs';
import { resolvers } from '../graphql/resolvers';

export const schema = makeExecutableSchema({
    typeDefs: TypeDefs,
    resolvers,
    logger: { log: e => console.log(e) },
});

类型定义.ts

import { types } from '../../graphql/types';
import { Types } from '../types/types';

const schemaDefinition = `
    schema {
        query         : Query
        mutation      : Mutation
    }
`;

const TypeDefs = [
    schemaDefinition,
    // Card Type Def
    Types.cardTypes.query,
    Types.cardTypes.mutation,

    // User Type Def
    Types.userTypes.query,
    Types.userTypes.mutation,

    ...types,
];

export { TypeDefs as default };

推荐阅读