mirror of
https://github.com/nature-of-code/noc-book-2
synced 2024-11-16 07:47:48 +01:00
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
module.exports = ({ createResolvers }) => {
|
|
// Add images field
|
|
const resolvers = {
|
|
BookSection: {
|
|
images: {
|
|
type: ['File'],
|
|
resolve: async (source, args, context, info) => {
|
|
const { entries } = await context.nodeModel.findAll({
|
|
query: {
|
|
filter: {
|
|
relativeDirectory: {
|
|
eq: `images/${/[^/]*(?=.html)/.exec(source.src)[0]}`,
|
|
},
|
|
internal: { mediaType: { regex: '/^(image)/' } },
|
|
},
|
|
},
|
|
type: 'File',
|
|
});
|
|
return entries;
|
|
},
|
|
},
|
|
},
|
|
|
|
Example: {
|
|
screenshot: {
|
|
type: 'File',
|
|
resolve: async (source, args, context, info) => {
|
|
const screenshot = await context.nodeModel.findOne({
|
|
type: 'File',
|
|
query: {
|
|
filter: {
|
|
relativeDirectory: {
|
|
eq: source.relativeDirectory,
|
|
},
|
|
internal: { mediaType: { regex: '/^(image)/' } },
|
|
},
|
|
},
|
|
});
|
|
return screenshot;
|
|
},
|
|
},
|
|
},
|
|
|
|
Exercise: {
|
|
screenshot: {
|
|
type: 'File',
|
|
resolve: async (source, args, context, info) => {
|
|
const screenshot = await context.nodeModel.findOne({
|
|
type: 'File',
|
|
query: {
|
|
filter: {
|
|
relativeDirectory: {
|
|
eq: source.relativeDirectory,
|
|
},
|
|
internal: { mediaType: { regex: '/^(image)/' } },
|
|
},
|
|
},
|
|
});
|
|
return screenshot;
|
|
},
|
|
},
|
|
},
|
|
};
|
|
createResolvers(resolvers);
|
|
};
|