Apollo graphql:makeExecutableSchema&;操场

Apollo graphql:makeExecutableSchema&;操场,graphql,apollo-server,prisma,Graphql,Apollo Server,Prisma,我是一个试图用apollo express和prisma建立graphql API的初学者 一切进展顺利,但随后我决定使用此库添加输入验证: graphql约束指令 它要求我使用makeExecutableSchema来构建模式,这样我就可以使用schemaDirectives参数 我启动服务器的代码如下所示: const server = new ApolloServer({ typeDefs, resolvers, context: ({req}) => {

我是一个试图用apollo express和prisma建立graphql API的初学者

一切进展顺利,但随后我决定使用此库添加输入验证: graphql约束指令

它要求我使用makeExecutableSchema来构建模式,这样我就可以使用schemaDirectives参数

我启动服务器的代码如下所示:

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: ({req}) => {
        const userId = getUserID(req);

        return {
            userId,
            prisma
        }
    }
});
const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
    schemaDirectives: {constraint: ConstraintDirective}
});
const server = new ApolloServer({
    schema,
    context: ({req}) => {
        const userId = getUserID(req);

        return {
            userId,
            prisma
        }
    }
});
一切都很顺利

但为了使用该库,我让它重构如下:

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: ({req}) => {
        const userId = getUserID(req);

        return {
            userId,
            prisma
        }
    }
});
const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
    schemaDirectives: {constraint: ConstraintDirective}
});
const server = new ApolloServer({
    schema,
    context: ({req}) => {
        const userId = getUserID(req);

        return {
            userId,
            prisma
        }
    }
});
它也能工作,我所有的查询和变异都能工作,验证也能工作

但它打破了graphql的游戏场:它不再能够加载我的模式和文档,两个选项卡都是空的。并显示以下错误: 无法访问服务器

它仍然可以工作:我可以发送我的查询和突变,但我不再有代码完成和自动文档,因为它不知道我的模式,所以不再有用

如果我替换纯typeDefs ans解析器的可执行模式,那么它会再次正常工作,Playdry会再次加载所有内容


在使用makeExecutableSchema时,我应该做些不同的事情吗,playgroun是否正常工作?

无论您是使用
makeExecutableSchema
还是将
typeDefs
Resolver
直接传递给
ApolloServer
构造函数都不重要——Apollo Server在引擎盖下使用
makeExecutableSchema
。实际上,您可以直接将指令映射传递给构造函数:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  schemaDirectives: {constraint: ConstraintDirective}
  context: ({req}) => {
    const userId = getUserID(req);

    return {
      userId,
      prisma
    }
  }
});
这是您正在使用的库中的错误。该指令将内置标量替换为自定义标量,但实际上不会将这些自定义标量添加到模式中。当Playway尝试内省模式时,它在内省结果中找不到自定义标量,并且出错。我会避免使用那个特定的库--它看起来不像是被积极维护的