Replies: 1 comment
-
|
Let me add more details here in case no one read the repo readme and looked at the example. Again, the issue is that GraphQL queries that sort based on a resolver field do not always appear to correctly run the resolver and use cached values. Our sources are multiple In exports.createSchemaCustomization = ({ actions }) => {
const schema = `
type VideosJson implements Node {
order: Int!
}
`;
actions.createTypes(schema);
};
exports.createResolvers = ({ createResolvers }) => {
createResolvers({
VideosJson: {
order: {
type: 'Int!',
resolve: async (source, args, context, info) => {
const { order } = await context.nodeModel.findOne({
type: `ContentJson`,
});
const { relativePath } = await context.nodeModel.getNodeById({ id: source.parent });
const index = order.indexOf(relativePath);
return index !== -1 ? index : 999;
},
},
},
});
};And in the import { graphql } from 'gatsby';
import * as React from 'react';
const IndexPage = ({ data }) => {
return (
<main>
<div>
{data.videos.nodes.map(({ title, order }) => (
<div key={title}>
Title: {title} | Order: {order}
</div>
))}
</div>
</main>
);
};
export default IndexPage;
export const query = graphql`
query {
videos: allVideosJson(sort: { order: ASC }) {
nodes {
title
order
}
}
}
`;If we change the file order in
I have also observed the same behavior in GraphiQL if you run the same query. The resolver returns the correct value, but the sorting order is wrong (cached?). |
Beta Was this translation helpful? Give feedback.



Uh oh!
There was an error while loading. Please reload this page.
-
Here's a simple repo to illustrate the problem: https://github.com/fturmel/gatsby-sort-resolver-bug
I think it's a bug, but maybe I'm missing something? I'd love some insights. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions