Apollo graphql:makeExecutableSchema 和 playground
Apollo graphql: makeExecutableSchema & playground
我是初学者,正在尝试使用 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
}
}
});
它也有效,我所有的查询和变更都有效,验证也有效。
但它破坏了 graphql-playground:它不再能够加载我的模式和文档,两个选项卡都是空的。它显示以下错误:
无法访问服务器
它仍然有效:我能够发送我的查询和变更等等,但是我不再有代码完成和自动文档,因为它知道我的模式,所以不再有用
如果我替换纯 typeDefs ans 解析器的可执行模式,它会再次正常工作,playground 会再次加载所有内容
我是否应该在使用 makeExecutableSchema 时做一些不同的事情来让 playgroun 工作?
无论您是使用 makeExecutableSchema
还是将 typeDefs
和 resolvers
直接传递给 ApolloServer
构造函数都无关紧要——Apollo Server 使用 makeExecutableSchema
无论如何在引擎盖下。其实直接把指令映射传给构造函数就可以了:
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {constraint: ConstraintDirective}
context: ({req}) => {
const userId = getUserID(req);
return {
userId,
prisma
}
}
});
这是您使用的库的错误。该指令将内置标量替换为自定义标量,但实际上并未将这些自定义标量添加到架构中。当 Playground 尝试自省模式时,它无法在自省结果中找到自定义标量并出错。我会避开那个特定的库——它看起来不像是在积极维护。
我是初学者,正在尝试使用 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
}
}
});
它也有效,我所有的查询和变更都有效,验证也有效。
但它破坏了 graphql-playground:它不再能够加载我的模式和文档,两个选项卡都是空的。它显示以下错误: 无法访问服务器
它仍然有效:我能够发送我的查询和变更等等,但是我不再有代码完成和自动文档,因为它知道我的模式,所以不再有用
如果我替换纯 typeDefs ans 解析器的可执行模式,它会再次正常工作,playground 会再次加载所有内容
我是否应该在使用 makeExecutableSchema 时做一些不同的事情来让 playgroun 工作?
无论您是使用 makeExecutableSchema
还是将 typeDefs
和 resolvers
直接传递给 ApolloServer
构造函数都无关紧要——Apollo Server 使用 makeExecutableSchema
无论如何在引擎盖下。其实直接把指令映射传给构造函数就可以了:
const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {constraint: ConstraintDirective}
context: ({req}) => {
const userId = getUserID(req);
return {
userId,
prisma
}
}
});
这是您使用的库的错误。该指令将内置标量替换为自定义标量,但实际上并未将这些自定义标量添加到架构中。当 Playground 尝试自省模式时,它无法在自省结果中找到自定义标量并出错。我会避开那个特定的库——它看起来不像是在积极维护。