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

feat(toSDL): allow custom sorting and sorting by entity types #332

Merged
merged 6 commits into from
May 11, 2021
Merged

feat(toSDL): allow custom sorting and sorting by entity types #332

merged 6 commits into from
May 11, 2021

Conversation

rhengles
Copy link
Contributor

@rhengles rhengles commented May 2, 2021

I know this will very likely need refactoring before being accepted, but I want some input if this feature is desired.

By default, when you call mySchemaComposer.toSDL(), all objects are printed to a string, sorted by the name of the entity (be it scalar, enum, interface, input, object or union). I think a much more human friendly way of printing it is:

  1. Scalar types
  2. Root Object types (Query, Mutation, Subscription)
  3. Enum types
  4. Interface types
  5. Input types
  6. Object types
  7. Union types

Actually I have almost no experience in GraphQL, I'm starting to learn and use it, but anyway I didn't like that the default way of printing mixes all these entity types. Maybe for an experienced GraphQL user the "ideal" way to sort entities when printing could be different, and the default way to sort entities by type could be changed in that case.

But what do you think?

How to use it?

import { SchemaComposer, printSortOptionsByType } from 'graphql-compose';

const schemaComposer = new SchemaComposer();

// build your schema

console.log(schemaComposer.toSDL({
	sortCustomOptions: printSortOptionsByType,
}));

Copy link
Member

@nodkz nodkz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhengles great idea for custom sorting 👍
I agree that alphabetic+kind sorting is a good feature.

But we need to

  • polish options (avoid simultaneous using of new options)
  • gives better names for option and new type definitions
  • add tests (it's the most important part!)
  • try to simplify life for developers when they use custom built-in sorting (avoid importing built-in function in the app code via providing custom sorting name as a string)

src/utils/schemaPrinter.ts Outdated Show resolved Hide resolved
src/utils/schemaPrinter.ts Outdated Show resolved Hide resolved
src/utils/schemaPrinter.ts Outdated Show resolved Hide resolved
@rhengles
Copy link
Contributor Author

rhengles commented May 6, 2021

Thank you for your review and your directions! I'm very honored that you're willing to merge my suggestion, including the specific sorting order. But there's no need for two different sorting by types inside the package, at least from my point of view of course, we can replace the sorting by types with your suggestion (root, scalar, etc).

I'll do the changes you requested. Thank you again so much!

@rhengles
Copy link
Contributor Author

rhengles commented May 6, 2021

@nodkz

  • add tests (it's the most important part!)

I completely agree about this, I just want to reach an agreement first about the public interface, so that I won't need to waste time rewriting tests 😁

I've pushed another commit applying your suggestions (with some slight modifications 😅), what do you think now? Can you review it ?

Thanks a lot again for your attention.

@rhengles rhengles requested a review from nodkz May 6, 2021 18:48
src/utils/sortTypes.ts Outdated Show resolved Hide resolved
Copy link
Member

@nodkz nodkz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good 👍
It's time to write tests 😉

@rhengles
Copy link
Contributor Author

rhengles commented May 8, 2021

@nodkz

I did some further simplification and added tests, please let me know if it is conforming to expectations.

Thanks

@rhengles rhengles requested a review from nodkz May 8, 2021 00:52
const res = [];
if (!omitDirectiveDefinitions) {
const directives = sc._directives.filter((d) => !BUILT_IN_DIRECTIVES.includes(d));
res.push(...directives.map((d) => printDirective(d, innerOpts)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to tests printing of Directives was broken:

Screen Shot 2021-05-11 at 01 45 52

Did you run all tests locally?

Copy link
Contributor Author

@rhengles rhengles May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nodkz You're right, I didn't, I only ran npx jest SchemaComposer-test.ts because my machine is slow, but after I finished I should have run them all.

I just pushed another commit fixing them, the tests passed on my machine this time, can you review it ?

if (p1[i] < p2[i]) return -1;
if (p1[i] > p2[i]) return +1;
}
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that [1, pos] is more expensive in runtime than 1 + rootPos/10. And comparePositionLists can be simplified to work with floats rather than arrays with loops/length checks:

function comparePositionLists(
  p1: number,
  p2: number,
): CompareTypeComposersResult {
  if (p1 < p2) return -1;
  if (p1 > p2) return +1;
  return 0;
}

Or maybe I something missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's a tricky one. I always like to make functions as generic as possible to enable reuse (or future modification), but in this case I guess it's not warranted 😅

As I said before, I'm not an expert on GraphQL, so I don't know if it's possible that one can have 10 or more root objects in the schema, in which case the sorting would break. That's the only reason I'd want to avoid a float.

But what do you think about this?

function comparePositionLists(
  p1: number[],
  p2: number[],
): CompareTypeComposersResult {
  if (p1[0] < p2[0]) return -1;
  if (p1[0] > p2[0]) return +1;
  if (p1[1] < p2[1]) return -1;
  if (p1[1] > p2[1]) return +1;
  return 0;
}

exclude?: string[];
}
): string {
opts = {
sortTypes: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I so much like your sorting algorithm ❤️ that I want to make sortTypes = 'GROUP_BY_TYPE' by default in schemaComposer.toSDL().

v8.0.0 was published just 1-2 weeks ago, so we may introduce this breaking change under v8.1.0 (not under v9.0.0).

@nodkz
Copy link
Member

nodkz commented May 10, 2021

@rhengles could you please allow for Maintainers to add changes in your Pull Request.
I wanted to add junit reporter with annotations in Github interface for broken tests.

@rhengles
Copy link
Contributor Author

@nodkz Thank you! I never used that option before ("allow mantainers add changes"). From what I searched online, it should appear right on this page? But there is not that option here. ( print: https://prnt.sc/12rxrek )

Yes, my machine is not very fast so I ran the test with npx jest SchemaComposer-test.ts, sorry for not running them all.

I also would like if the sorting group_by_type was the default option 😀

I'll see each reply above, just wanted to give you a heads up about the setting you asked.

@nodkz nodkz merged commit 933d0f8 into graphql-compose:master May 11, 2021
@nodkz
Copy link
Member

nodkz commented May 11, 2021

I never used that option before ("allow mantainers add changes"). From what I searched online, it should appear right on this page? But there is not that option here.

Yeah, it's strange that you don't have this checkbox. Anyway when you open Pull Request checkbox (allow mantainers add changes) is still available.

I've just merged your changes in master and I'll make final changes during the day and publish a new package version. Thanks a lot for your solid improvements. I was very pleased to work with you! 🤩

PS. If you want to make improvements according to these changes or any other – feel free to open a new Pull Request. Thanks again 🙏💪

@github-actions
Copy link

🎉 This PR is included in version 8.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@rhengles rhengles deleted the feat/print-order branch May 11, 2021 08:58
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 this pull request may close these issues.

None yet

2 participants