Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL Orderby not working #17475

Closed
zyr2288 opened this issue Feb 11, 2025 · 3 comments · Fixed by #17476
Closed

GraphQL Orderby not working #17475

zyr2288 opened this issue Feb 11, 2025 · 3 comments · Fixed by #17476
Labels

Comments

@zyr2288
Copy link

zyr2288 commented Feb 11, 2025

I set createdUtc to DESC, but the sorting is not correct.

query MyQuery {
  article(orderBy: {createdUtc: DESC}) {
    displayText
    createdUtc
  }
}
{
  "data": {
    "article": [
      {
        "displayText": "First",
        "createdUtc": "2025-02-11T04:51:32.6114111Z"
      },
      {
        "displayText": "Second",
        "createdUtc": "2025-02-11T04:51:41.4293335Z"
      }
    ]
  }
}

In the source

    private static IQuery<ContentItem, ContentItemIndex> OrderBy(IQuery<ContentItem, ContentItemIndex> query,
        IResolveFieldContext context)
    {
        if (context.HasPopulatedArgument("orderBy"))
        {
            var orderByArguments = JObject.FromObject(context.Arguments["orderBy"].Value);

            if (orderByArguments != null)
            {
                var thenBy = false;

                foreach (var property in orderByArguments)
                {
                    // Maybe there's something wrong with this line in the source code
                    // property.Value -> Descending
                    // direction -> Ascending
                    var direction = (OrderByDirection)property.Value.Value<int>();

                    Expression<Func<ContentItemIndex, object>> selector = null;

                    switch (property.Key)
                    {
                        case "contentItemId": selector = x => x.ContentItemId; break;
                        case "contentItemVersionId": selector = x => x.ContentItemVersionId; break;
                        case "displayText": selector = x => x.DisplayText; break;
                        case "published": selector = x => x.Published; break;
                        case "latest": selector = x => x.Latest; break;
                        case "createdUtc": selector = x => x.CreatedUtc; break;
                        case "modifiedUtc": selector = x => x.ModifiedUtc; break;
                        case "publishedUtc": selector = x => x.PublishedUtc; break;
                        case "owner": selector = x => x.Owner; break;
                        case "author": selector = x => x.Author; break;
                    }

                    if (selector != null)
                    {
                        if (!thenBy)
                        {
                            query = direction == OrderByDirection.Ascending
                                    ? query.OrderBy(selector)
                                    : query.OrderByDescending(selector)
                                ;
                        }
                        else
                        {
                            query = direction == OrderByDirection.Ascending
                                    ? query.ThenBy(selector)
                                    : query.ThenByDescending(selector)
                                ;
                        }

                        thenBy = true;
                    }
                }
            }
        }
        else
        {
            query = query.OrderByDescending(x => x.CreatedUtc);
        }

        return query;
    }
Copy link
Contributor

Thank you for submitting your first issue, awesome! 🚀 We're thrilled to receive your input. If you haven't completed the template yet, please take a moment to do so. This ensures that we fully understand your feature request or bug report. On what happens next, see the docs.

If you like Orchard Core, please star our repo and join our community channels.

@zyr2288
Copy link
Author

zyr2288 commented Feb 11, 2025

Wouldn't it be better to change it to something like this?

var direction = Enum.Parse<OrderByDirection>(property.Value.Value<string>());

@gvkries
Copy link
Contributor

gvkries commented Feb 11, 2025

You're absolutely right, nice finding. I've seen this error too, but forgot about it. I guess this happened due to updating to STJ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants