【问题标题】:Make typedef in a separate file to import在单独的文件中制作 typedef 以导入
【发布时间】:2020-01-06 18:10:48
【问题描述】:

我正在使用 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.js graphql


    【解决方案1】:

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

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

    schema.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) },
    });
    

    typedefs.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 };
    

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 2017-08-28
      • 2016-04-03
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2023-02-02
      相关资源
      最近更新 更多