使用 ApolloServer 从 graphql-import 迁移到 graphql-tools 时遇到麻烦,指令停



我的困境始于一个简单的愿望,即将我的 graphql 模式从单个 .graphql 文件扩展到多个文件,这样我就可以更好地组织模式,这样它就会;t增长到一个巨大的文件失控。

我最初的布局非常简单,我在schema.graphql文件中有一个工作架构。我将能够使用 graphql-import 库中的importSchema('server/schema.graphql')将其解析为字符串,该库现已弃用 https://github.com/ardatan/graphql-import

他们提到它已在最新版本中合并到graphql-tools中,并在此处提供了迁移教程 https://www.graphql-tools.com/docs/migration-from-import 本教程看起来非常简单,因为他们的第一个示例几乎准确地说明了我的代码是什么样子的(除了我不使用 es6 import 但旧的 fashoined require(:

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函数返回一个对象而不是字符串文字,我需要在makeExecutableSchema中传递给typeDefs

我正在使用 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,
});

这允许我的服务器运行,我可以执行查询和更改,但是,我的指令不再工作,并且我不再对受保护的查询进行身份验证。

我想要一种方法来导入我的 .graphql 文件并将其解析为字符串,以便我可以像以前使用 importSchema(( 一样在typeDefs内使用它,或者一种在不使用 makeExecutableSchema(( 的情况下解声明我的指示的方法,以便它们再次继续工作!

我已经上下浏览了文档并查看了其他库,到目前为止,我一直在做短,非常感谢任何提示或指导

makeExecutableSchema仍然是graphql-tools的一部分,您可以继续使用它,如文档中所示。文档中显示的示例的问题在于它不等同于您之前所做的。您应该改用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);

我尝试过这种方法,但我无法解决问题。一个独特的解决方案,设法采取以下方法:

const { ApolloServer, makeExecutableSchema, gql} = require('apollo-server-express')
const { loadTypedefsSync }  = require('@graphql-tools/load')
const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader')
const path = require('path')
const sources = loadTypedefsSync(
path.resolve(__dirname, '../schema/root.graphql'),
{ loaders: [new GraphQLFileLoader()] }
)
const typeDefs = sources.map(source => source.document)
const schema = makeExecutableSchema({
typeDefs: gql`${typeDefs[0]}`,
resolvers,
})

我遇到了同样的问题,我通过.graphql加载了模式,我想添加 graphql-constraint-directive。我的解决方案是使用loadSchemaSync加载架构,然后使用wrapSchema使用转换函数,您还需要将指令添加到您的.graphql文件之一中:

import { addResolversToSchema, wrapSchema } from 'graphql-tools';
import { GraphQLSchema } from 'graphql';
import resolvers from './resolver';
schema = loadSchemaSync('./**/*.graphql', {
loaders: [new GraphQLFileLoader()],
});
const schemaWithResolver = addResolversToSchema({
schema,
resolvers
});
const { constraintDirective } = require('graphql-constraint-directive')

const schemaConstrain = wrapSchema({
schema: schemaWithResolver,
transforms: [constraintDirective()]
})

架构包装文档

最新更新