P@
Ну плюс есть выход наManning Publications. Я там технический ревьювир для курса https://www.manning.com/livevideo/graphql-in-motion (они его уже больше года пилят и еще не закончили, уже два автора сменилось 😅).
Size: a a a
P@
P@
U
P@
js
const ArticleType = new GraphQLObjectType({
name: 'Article',
fields: () => ({
title: { type: GraphQLString },
authorId: { type: GraphQLString },
author: {
type: AuthorType,
// WAS
// resolve: source => {
// return authorModel.findById(source.authorId);
// },
// BECAME:
resolve: (source, args, context, info) => {
// See dl-server.js, there was created context = { dataloaders: new WeakMap() };
const { dataloaders } = context;
// init DataLoader once, if not exists
// we use a MAGIC here
// `Set.get(info.fieldNodes)` is unique for every field in the query
// it helps to determine the same resolvers
let dl = dataloaders.get(info.fieldNodes);
if (!dl) {
dl = new DataLoader(async (ids: any) => {
// regular request to our database
const rows = await authorModel.findByIds(ids);
// IMPORTANT: we MUST return rows in the same order like we get ids
const sortedInIdsOrder = ids.map(id => rows.find(x => x.id === id));
return sortedInIdsOrder;
});
dataloaders.set(info.fieldNodes, dl);
}
// load author via dataloader
return dl.load(source.authorId);
},
},
}),
});
P@
MM
MM
City
с полями name_en
, name_ru
, это тип находится в дочерней схеме, я могу сделать extend и добавить поле name, а также добавить резолвер на это поле. Но если сам пользователь не запросит поля name_en и name_ru, я в аргументе parent моего резолвера не получу этих полейMM
OG
MM
OG
MM
OG
MM
MM
OG
MM
MM
MM
City
с полями name_en
, name_ru
, это тип находится в дочерней схеме, я могу сделать extend и добавить поле name, а также добавить резолвер на это поле. Но если сам пользователь не запросит поля name_en и name_ru, я в аргументе parent моего резолвера не получу этих полейIn order to delegate to these root fields, we’ll need to make sure we’ve actually requested the id of the user or the authorId of the chirp. To avoid forcing users to add these fields to their queries manually, resolvers on a merged schema can define a fragment property that specifies the required fields, and they will be added to the query automatically.
MM