首页 > 解决方案 > 在 GraphQL Resolvers 文件中定义一个函数

问题描述

我想在定义解析器的同一个 ts 文件中定义一个函数

export const resolvers = {
    Query: {
        books: () => {
            return [
                {
                    title: 'Harry Potter and the Chamber of Secrets',
                    author: 'J.K. Rowling',
                },
                {
                    title: 'Jurassic Park',
                    author: 'Michael Crichton',
                },
            ];
        },
    },
};

private export (id: string): boolean {
    ...
    return true;
}

但我得到一个编译错误

TS2304: Cannot find name 'export'.

标签: javascriptnode.jstypescriptgraphql

解决方案


这是因为您的函数声明无效。你可以这样尝试:

private export function someName(id: string): boolean {

    return true;
}

但是你会得到另一个错误:

'private' modifier cannot appear on a module or namespace element.

所以最终的解决方案是:

export function someName(id: string): boolean {

    return true;
}

推荐阅读