如何在 GraphQL 中获取对等字段
How to obtain peer fields in GraphQL?
>想象一下我们有 GraphQL 方案,其中某些字段依赖于父类型中未解析的对等字段。 foo
和bar
下面的代码片段。
const MyType = new GraphQLObjectType({
name 'MyType'
fields {
id: {
type: GraphQLString // Resolved from parent type
},
foo: {
type: GraphQLString,
description: 'Foo',
resolve: function({id}, args, {rootValue:{logger, datasources:{pool}}}) {
// Making some queries to database
}
},
bar: {
type: GraphQLString,
description: 'Bar',
resolve: function({id}, args, {rootValue:{logger, datasources:{pool}}}) {
// Making some queries to database
}
},
baz: {
type: GraphQLString,
description: 'Baz',
resolve: function({id}, args, {rootValue:{logger, datasources:{pool}}}) {
// This field actually depends from 'foo' and 'bar' peers.
// How to obtain peer fields 'foo' and 'bar to resolve this field?
// We have access to 'id' here, but this field resolved from parent type
// and we can't access to 'foo' and 'bar' like we do it for 'id'.
}
}
}
})
如何在其他对等字段中访问此字段?
每个字段都必须可以单独解析。
const fooResolver = function(source, args, info) {
// Making some queries to database
};
const barResolver = function(source, args, info) {
// Making some queries to database
};
const bazResolver = function(source, args, info) {
const foo = fooResolver(source, args, info);
const bar = barResolver(source, args, info);
// TODO: Resolve baz.
};
const MyType = new GraphQLObjectType({
name 'MyType'
fields {
id: {
type: GraphQLString, // Resolved from parent type
},
foo: {
type: GraphQLString,
description: 'Foo',
resolve: fooResolver,
},
bar: {
type: GraphQLString,
description: 'Bar',
resolve: barResolver,
},
baz: {
type: GraphQLString,
description: 'Baz',
resolve: bazResolver,
},
},
});
您会注意到,如果您获取 foo
、 baz
和 bar
,则其性能特征很差。要解决此问题,请使用数据加载程序。例如,使用facebook/dataloader:
function cacheKeyFn(input) {
// Produce a stable cache key string from the input
}
const fooLoader = new DataLoader(input => {
// Return a Promise that fetches `foo` given `input`
}, {cacheKeyFn});
const barLoader = new DataLoader(input => {
// Return a Promise that fetches `bar` given `input`
}, {cacheKeyFn});
const bazLoader = new DataLoader(input => {
// Return a Promise that fetches `baz` given `input`
}, {cacheKeyFn});
const fooResolver = function(source, args, info) {
return await fooLoader.load(source, args, info);
};
const barResolver = function(source, args, info) {
return await barLoader.load(source, args, info);
};
const bazResolver = function(source, args, info) {
return await bazLoader.load(source, args, info);
};
const bazResolver = function(source, args, info) {
const foo = await fooResolver(source, args, info);
const bar = await barResolver(source, args, info);
return await bazResolver({...source, foo, bar}, args, info);
};
相关文章:
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- jquery试图按名称获取按钮位置
- 如何在jQuery中获取元素的形式
- 如何在php文件中获取$.post-ajax传递的值
- 在Shopify中获取博客文章的图片
- 使用Javascript获取所选选项ID
- 在jQuery中获取表的行索引
- 使用jquery将mysql数据获取到新的表行中
- 在动态创建的元素上获取对特定选择器的引用
- 从城市名称获取惊喜
- Angular只从数组中获取所需的数据
- 无法将数据从firebase获取到我的html页面
- 从ajax请求中获取javascript对象
- 如何从画布上的某个移动事件中获取X和Y
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 如何在PHP中使用$_POST获取Select元素值
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 如何在 GraphQL 中获取对等字段