我的困境始于将我的 graphql 模式从单个 .graphql 文件扩展到多个文件的简单愿望,这样我就可以更好地组织模式,这样它就不会变成一个失控的大文件。
我的原始布局非常简单,我在 schema.graphql
中有一个工作模式文件。我将能够使用 importSchema('server/schema.graphql')
将其解析为字符串来自 graphql-import 库,现在已弃用 https://github.com/ardatan/graphql-import
他们提到它已合并到graphql-tools
在最新版本中并在此处提供迁移教程 https://www.graphql-tools.com/docs/migration-from-import该教程看起来非常简单,因为他们的第一个示例几乎准确地说明了我的代码的样子(除了我不使用 es6 导入但旧的要求):
import { importSchema } from 'graphql-import';
import { makeExecutableSchema } from 'graphql-tools';
const typeDefs = importSchema(join(__dirname, 'schema.graphql'));
const resolvers = {
Query: {...}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
然后他们说要修改它,只需进行这些更改
import { loadSchemaSync } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { addResolversToSchema } from '@graphql-tools/schema';
const schema = loadSchemaSync(join(__dirname, 'schema.graphql'), { loaders: [new GraphQLFileLoader()] });
const resolvers = { Query: {...} };
const schemaWithResolvers = addResolversToSchema({
schema,
resolvers,
});
我做了这些更改,但重要的区别是它们不再使用
makeExecutableSchema()
在他们的示例中,这对我来说非常重要,因为我需要包含指令。我现在如何处理架构?我如何声明指令?他们的指令文档仍然使用 makeExecutableSchema
但自从新的 loadSchemaSync
以后我不能再用它了函数返回一个对象而不是字符串文字,我需要将其传递给 typeDefs
在 makeExecutableSchema
我正在使用 apollo-server,所以似乎一种可能的解决方法是在 apollo-server 构造函数中声明指令,然后传入这个新的
schemaWithResolvers
作为这样的模式const server = new ApolloServer({
schema, //this includes now the returned value of using addResolversToSchema()
schemaDirectives : {
auth:AuthDirective,
authRole: AuthRoleDirective
}
context : ({req}) => //dostuff,
});
这允许我的服务器运行,并且我可以执行查询和更改,但是,我的指令不再有效,并且我不再对 protected 查询进行身份验证。
我想要一种导入我的 .graphql 文件并将其解析为字符串的方法,以便我可以在
typeDefs
中使用它正如我以前使用 importSchema() 或一种在不使用 makeExecutableSchema() 的情况下声明我的目录的方法,以便它们再次继续工作!我已经查看了文档并查看了其他库,到目前为止我一直很简短,非常感谢任何提示或指导
最佳答案
makeExecutableSchema
仍然是 graphql-tools
的一部分您可以继续使用它,如图 here在文档中。文档中显示的示例的问题在于它与您之前所做的不同。您应该使用 loadTypedefsSync
反而:
import { loadTypedefsSync } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { addResolversToSchema } from '@graphql-tools/schema';
const sources = loadTypedefsSync(join(__dirname, 'schema.graphql'), { loaders: [new GraphQLFileLoader()] });
const documentNodes = sources.map(source => source.document);
const resolvers = { Query: {...} };
const schema = makeExecutableSchema({ typeDefs, resolvers });
或者,如果你去
loadSchema
路线,您应该能够在加载后将指令应用于您的架构:import { SchemaDirectiveVisitor } from "@graphql-tools/utils";
import { loadSchemaSync } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { addResolversToSchema } from '@graphql-tools/schema';
const schema = loadSchemaSync(join(__dirname, 'schema.graphql'), { loaders: [new GraphQLFileLoader()] });
const resolvers = { Query: {...} };
const schemaWithResolvers = addResolversToSchema({
schema,
resolvers,
});
SchemaDirectiveVisitor.visitSchemaDirectives(schemaWithResolvers, schemaDirectives);
关于node.js - 使用 ApolloServer 从 graphql-import 迁移到仅 graphql-tools 时遇到问题,指令停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62441400/