如何在其构造函数之外访问 ApolloServer 数据源?
How to access an ApolloServer data source outside of its constructor?
我正在使用 apollo-datasource-rest
从 REST API 中获取数据。从文档中,您可以像这样集成数据源:
const server = new ApolloServer({
typeDefs,
dataSources: () => ({
ds1: new Datasource1()
}),
resolvers: {
Post: {
comments: (post, _args, { dataSources }) => {
dataSources.ds1.getComments(post.id)
}
}
}
})
我的问题是我更喜欢使用 类 来实现解析器,所以对于 Post 我有这样的东西:
class Post {
constructor(public id: number, public title: string, public content: string) {}
comments() {
return new Datasource1.getComments(this.id)
}
}
我想知道这是否受支持?
Datasource1
的多个实例可以共享同一个缓存吗?我不这么认为。
是否可以在其构造函数之外访问 ApolloServer
dataSources
?
因为这个又出现在评论里了。 GraphQL.js 允许使用两种不同的方式来定义解析器。您要么使用普通数据对象,要么在模式内部定义解析器逻辑。这就是大多数人使用 GraphQL 的方式,因为这是大多数文档显示的内容以及 Apollo 所宣扬的内容。第二种方式是依赖默认解析器并创建数据访问对象。让我快速展示一下我们如何想象一个领域在概念上得到解决:
// When the field specifies a resolver
if (typeof fieldConfig.resolver !== 'undefined') {
return fieldConfig.resolver(root, args, context, resolveInfo);
}
// When the field does not specify a resolver but the root value has a method with
// the same name
if (typeof root[fieldConfig.name] === 'function') {
return root[fieldConfig.name](args, context, resolveInfo);
}
// Otherwise we return the value under the name with the potential of it being undefined
return root[fieldConfig.name];
所以我们可以 return 带有方法的对象,就像 OP 对我们所有类型所做的那样。该对象包装静态属性,并具有用于需要参数或需要访问上下文的更复杂字段的方法。请注意在这种情况下如何不传递根值,因为该方法是在 根值上调用的。这意味着根值可以在方法内部作为 this
访问,就像我们习惯于从 OOP 访问一样。
直接回答问题:上下文可作为解析器方法中的第二个参数访问。具体的我们可以写如下代码:
class Post {
constructor(public id: number, public title: string, public content: string) {}
comments(_args, { datasources }) {
return datasources.ds1.getComments(this.id)
}
}
我正在使用 apollo-datasource-rest
从 REST API 中获取数据。从文档中,您可以像这样集成数据源:
const server = new ApolloServer({
typeDefs,
dataSources: () => ({
ds1: new Datasource1()
}),
resolvers: {
Post: {
comments: (post, _args, { dataSources }) => {
dataSources.ds1.getComments(post.id)
}
}
}
})
我的问题是我更喜欢使用 类 来实现解析器,所以对于 Post 我有这样的东西:
class Post {
constructor(public id: number, public title: string, public content: string) {}
comments() {
return new Datasource1.getComments(this.id)
}
}
我想知道这是否受支持?
Datasource1
的多个实例可以共享同一个缓存吗?我不这么认为。
是否可以在其构造函数之外访问 ApolloServer
dataSources
?
因为这个又出现在评论里了。 GraphQL.js 允许使用两种不同的方式来定义解析器。您要么使用普通数据对象,要么在模式内部定义解析器逻辑。这就是大多数人使用 GraphQL 的方式,因为这是大多数文档显示的内容以及 Apollo 所宣扬的内容。第二种方式是依赖默认解析器并创建数据访问对象。让我快速展示一下我们如何想象一个领域在概念上得到解决:
// When the field specifies a resolver
if (typeof fieldConfig.resolver !== 'undefined') {
return fieldConfig.resolver(root, args, context, resolveInfo);
}
// When the field does not specify a resolver but the root value has a method with
// the same name
if (typeof root[fieldConfig.name] === 'function') {
return root[fieldConfig.name](args, context, resolveInfo);
}
// Otherwise we return the value under the name with the potential of it being undefined
return root[fieldConfig.name];
所以我们可以 return 带有方法的对象,就像 OP 对我们所有类型所做的那样。该对象包装静态属性,并具有用于需要参数或需要访问上下文的更复杂字段的方法。请注意在这种情况下如何不传递根值,因为该方法是在 根值上调用的。这意味着根值可以在方法内部作为 this
访问,就像我们习惯于从 OOP 访问一样。
直接回答问题:上下文可作为解析器方法中的第二个参数访问。具体的我们可以写如下代码:
class Post {
constructor(public id: number, public title: string, public content: string) {}
comments(_args, { datasources }) {
return datasources.ds1.getComments(this.id)
}
}