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 object proc macro #333

Merged
merged 15 commits into from
May 14, 2019

Conversation

theduke
Copy link
Member

@theduke theduke commented Mar 8, 2019

This picks up form @ForsakenHarmony s work.

I squashed the commits down to one, but preserved the contribution.

Old pr: #181
( tried to push to @ForsakenHarmony s branch, but the force push apparently closed the old PR and I can't reopen it)

My commit finishes up the implementation.

Todo:

  • docs
  • some more tests
  • deprecate graphql_object! macro and replace it everywhere

NOTE: I decided to go for a different syntax than previously discussed.
Main motivation:

  • don't imply that you are actually implementing a trait
  • make IDEs work/not report errors.

Check the integration_tests/src/codegen/impl_object.rs file for the tests.

It looks like this:

#[juniper::impl_object(
  context = MyContext
)]
impl Query {
  fn whatever(ctx: &MyContext, arg: bool) -> bool {
    true
  }
}

@theduke theduke requested a review from LegNeato March 8, 2019 20:30
@theduke
Copy link
Member Author

theduke commented Mar 8, 2019

@ForsakenHarmony thanks for starting this, and sorry it took so long for us to get the ball rolling.

@theduke theduke force-pushed the graphql-object-proc-macro branch 2 times, most recently from 77febb5 to 651007e Compare March 8, 2019 20:54
@codecov-io
Copy link

codecov-io commented Mar 8, 2019

Codecov Report

Merging #333 into master will decrease coverage by 1.96%.
The diff coverage is 32.41%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #333      +/-   ##
==========================================
- Coverage   88.32%   86.36%   -1.97%     
==========================================
  Files         103      105       +2     
  Lines       14922    15348     +426     
==========================================
+ Hits        13180    13255      +75     
- Misses       1742     2093     +351
Impacted Files Coverage Δ
juniper/src/macros/tests/object.rs 92.15% <ø> (ø) ⬆️
juniper/src/macros/interface.rs 88.63% <ø> (-2.28%) ⬇️
integration_tests/juniper_tests/src/lib.rs 100% <ø> (ø) ⬆️
juniper_warp/src/lib.rs 93.17% <ø> (ø) ⬆️
juniper_iron/src/lib.rs 73.29% <ø> (ø) ⬆️
juniper/src/types/base.rs 91.01% <ø> (-0.7%) ⬇️
juniper/src/lib.rs 87.09% <ø> (ø) ⬆️
juniper/src/macros/object.rs 42.42% <ø> (-52.58%) ⬇️
juniper_codegen/src/derive_object.rs 0% <0%> (ø) ⬆️
juniper_codegen/src/impl_object.rs 0% <0%> (ø)
... and 43 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 794568e...a2b2cc3. Read the comment docs.

@LegNeato LegNeato added the enhancement Improvement of existing features or bugfix label Mar 9, 2019
@mwilliammyers
Copy link

@theduke Will this work with interfaces? It is currently not very ergonomic to have to implement a trait and use graphql_object!...

I might need to open a new issue but if using this with interfaces will not work then would it be possible to use a trait + graphql_interface! + GraphQLObject instead of a trait + graphql_interface! + a struct +graphql_object!

Copy link
Member

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Does this increase the minimum rust version?

  • Formatting is failing
  • No Changelog entry

juniper_codegen/src/util.rs Show resolved Hide resolved
@theduke
Copy link
Member Author

theduke commented Mar 23, 2019

@mwilliammyers interfaces are another topic, but we can definitely make these more convenient.

We can discuss in #295 .

@theduke theduke force-pushed the graphql-object-proc-macro branch 2 times, most recently from f1b42fd to e045ee4 Compare May 7, 2019 09:20
@theduke
Copy link
Member Author

theduke commented May 7, 2019

@LegNeato

Alright! This was a lot of work, but it's ready now.

  • impl_object now supports all features of graphql_object!
  • all uses of graphql_object are replaced with impl_object, except the macro tests
  • book is updated
  • impl_object has decent documentation

A couple of notes:

Rust version

Sadly the Rust version must be bumped to 1.33 to allow for arbitrary tokens in a attribute.

Diagnostics

Error messages could be improved quite a bit in some cases with RFC 1566, but that doesn't seem close to stabilization.

Deprecation

#[deprecated] has no effect on macros, so I've settled on a big manual deprecation warning

Argument customization

Argument customization is pretty awkward right now.
We can substantially improve this once RFC 2565 is implemented.

For now we'll have to live with:

#[graphql(
  arguments(
    arg1(
       description = "..",
       default = true
    )
  )
)]
fn field(arg1: bool) ->  bool { arg1 }

Re-exports

I've fixed up the re-exports in juniper/src/lib.rs
We do not want to export the internal macros...

Also, I've started to manually re-export all items so rustdoc shows documentation for them.
We should actually add some documentation on them too but that's another PR.

@theduke theduke force-pushed the graphql-object-proc-macro branch 3 times, most recently from d798d38 to a3eebda Compare May 7, 2019 10:36
@theduke theduke requested a review from LegNeato May 7, 2019 11:43
@theduke
Copy link
Member Author

theduke commented May 12, 2019

@LegNeato ping

@theduke theduke mentioned this pull request May 12, 2019
@theduke theduke force-pushed the graphql-object-proc-macro branch from a3eebda to 738a44e Compare May 12, 2019 08:32
This commit implements a new proc macro `impl_object` that replaces
the old graphql_object! macro.

The code shares a lot of similarities with the GraphQLObject
custom derive, so the code was unified to handle both
more generically.

Also, doc comment processing was standardized and improved.
This commit deprecates the graphql_object macro and replaces
all of it's uses with the new impl_object proc macro.
(Except for the old macro tests).

This commit also adds new integration tests for impl_object.
Remove the internal macros from re-export.
This was a mistake.
Also, import each item from juniper_codegen manually to enable
rustdoc integration.
@theduke theduke force-pushed the graphql-object-proc-macro branch 2 times, most recently from d7a45f6 to e5226de Compare May 12, 2019 08:49
@theduke theduke force-pushed the graphql-object-proc-macro branch from e5226de to de12e0e Compare May 12, 2019 08:50
Needed for releasing automation.
@theduke theduke force-pushed the graphql-object-proc-macro branch from 0ac8eaf to 552b4d0 Compare May 12, 2019 19:04
@LegNeato
Copy link
Member

LegNeato commented May 12, 2019

It uses a regex using "[^"]+" so I doubt that's it. Like I said I tested everything and was sure changing versions in codegen worked (even in CI) so something is wonky. As you can see on master the release job passed.

Patch juniper_codegen dev dependency on juniper.
@theduke
Copy link
Member Author

theduke commented May 12, 2019

Alright, found it. Needed to add the replacement config in juniper/release.toml.

Copy link
Member

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

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

Arguments look pretty ugly in this new world :-/. Also not really in love with impl_object name, but can't think of anything better right now.

juniper/src/macros/tests/field.rs Show resolved Hide resolved
juniper/src/macros/tests/object.rs Outdated Show resolved Hide resolved
juniper/src/macros/tests/union.rs Outdated Show resolved Hide resolved
@theduke
Copy link
Member Author

theduke commented May 13, 2019

Arguments look pretty ugly in this new world

Yeah as mentioned above, arguments can become nicer once the accepted RFC for attributes on function parameters is implemented. That will enable

fn field(
  &self,
  #[graphql(default = true, description = "arg")]
  arg: bool,
) -> &str {

}

Also not really in love with impl_object name, but can't think of anything better right now.

Me neither.

I initially stuck with impl_object because of the name clash with graphql_object!, but since that is already replaced and removed from the docs, I think just object is the better choice.

@theduke theduke force-pushed the graphql-object-proc-macro branch from a2b2cc3 to 29025e6 Compare May 13, 2019 19:15
@theduke
Copy link
Member Author

theduke commented May 14, 2019

Alright, let's do this.

PS: @LegNeato , please check gitter!

@theduke theduke merged commit 61f288b into graphql-rust:master May 14, 2019
@theduke theduke mentioned this pull request Jun 30, 2020
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement of existing features or bugfix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants