Skip to content

Commit 8664538

Browse files
committed
Add new GraphQL resolver
1 parent c61b59f commit 8664538

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

gatsby-node.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,15 @@ exports.createResolvers = ({
1313
grvscCodeBlocks: {
1414
type: ['GRVSCCodeBlock'],
1515
resolve(source, _, context) {
16-
return getFromCache();
17-
18-
/** @param {boolean=} stop */
19-
async function getFromCache(stop) {
20-
const childNodes = await getChildNodes(cache, source.id, source.internal.contentDigest);
21-
// Hack alert: ensure plugin has been run by querying htmlAst,
22-
// which is set via `setFieldsOnGraphQLNodeType` by gatsby-transformer-remark,
23-
// therefore might not have been run before this resolver runs.
24-
if (!childNodes && !stop) {
25-
await context.nodeModel.runQuery({
26-
query: {
27-
filter: {
28-
id: { eq: source.id },
29-
htmlAst: { ne: null },
30-
},
31-
},
32-
type: 'MarkdownRemark',
33-
firstOnly: true,
34-
});
35-
return getFromCache(true);
36-
}
37-
if (!childNodes) {
38-
logger.error(
39-
'gatsby-remark-vscode couldn’t retrieve up-to-date GRVSCCodeBlock GraphQL nodes. ' +
40-
'The `GRVSCCodeBlocks` field may be missing, empty or stale. ' +
41-
'The Gatsby cache is probably in a weird state. Try running `gatsby clean`, and file an ' +
42-
'issue at https://github.com/andrewbranch/gatsby-remark-vscode/issues/new if the problem persists.'
43-
);
44-
45-
return context.nodeModel.runQuery({
46-
query: { parent: { id: { eq: source.id } } },
47-
type: 'GRVSCCodeBlock',
48-
firstOnly: false
49-
});
50-
}
51-
return childNodes || [];
52-
}
53-
},
16+
return getFromCache('GRVSCCodeBlock', cache, source, context);
17+
}
5418
},
19+
grvscCodeSpans: {
20+
type: ['GRVSCCodeSpan'],
21+
resolve(source, _, context) {
22+
return getFromCache('GRVSCCodeSpan', cache, source, context);
23+
}
24+
}
5525
},
5626

5727
Query: {
@@ -83,3 +53,45 @@ exports.createResolvers = ({
8353
}
8454
});
8555
};
56+
57+
/**
58+
* @param {string} type
59+
* @param {any} cache
60+
* @param {any} source
61+
* @param {any} context
62+
* @param {boolean=} stop
63+
*/
64+
async function getFromCache(type, cache, source, context, stop) {
65+
const childNodes = await getChildNodes(cache, source.id, source.internal.contentDigest);
66+
// Hack alert: ensure plugin has been run by querying htmlAst,
67+
// which is set via `setFieldsOnGraphQLNodeType` by gatsby-transformer-remark,
68+
// therefore might not have been run before this resolver runs.
69+
if (!childNodes && !stop) {
70+
await context.nodeModel.runQuery({
71+
query: {
72+
filter: {
73+
id: { eq: source.id },
74+
htmlAst: { ne: null },
75+
},
76+
},
77+
type: 'MarkdownRemark',
78+
firstOnly: true,
79+
});
80+
return getFromCache(cache, source, context, true);
81+
}
82+
if (!childNodes) {
83+
logger.error(
84+
'gatsby-remark-vscode couldn’t retrieve up-to-date GRVSCCodeBlock GraphQL nodes. ' +
85+
'The `GRVSCCodeBlocks` field may be missing, empty or stale. ' +
86+
'The Gatsby cache is probably in a weird state. Try running `gatsby clean`, and file an ' +
87+
'issue at https://github.com/andrewbranch/gatsby-remark-vscode/issues/new if the problem persists.'
88+
);
89+
90+
return context.nodeModel.runQuery({
91+
query: { parent: { id: { eq: source.id } } },
92+
type,
93+
firstOnly: false
94+
});
95+
}
96+
return childNodes || [];
97+
}

0 commit comments

Comments
 (0)