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

Allow #[derive(GraphQLInterface)] and #[graphql_interface] on structs #1026

Merged
merged 128 commits into from
Apr 1, 2022

Conversation

ilslv
Copy link
Member

@ilslv ilslv commented Feb 21, 2022

Synopsis

After #1009 GraphQL interfaces are represented with traits acting as blueprints for methods names, arguments and return types.

Solution

To emphasise structural nature of GraphQL interfaces, allow representing them with structs:

#[derive(GraphQLInterface)]
#[graphql(for = Human)]
struct Character {
    id: String,
}
#[graphql_interface(for = Human)]
struct Character {
    id: String,
}

Checklist

  • Created PR:
    • In draft mode
    • Name contains Draft: prefix
    • Name contains issue reference
    • Has assignee
  • Documentation is updated (if required)
  • Tests are updated (if required)
  • Changes conform code style
  • CHANGELOG entry is added (if required)
  • FCM (final commit message) is posted
    • and approved
  • Review is completed and changes are approved
  • Before merge:
    • Milestone is set
    • PR's name and description are correct and up-to-date
    • Draft: prefix is removed
    • All temporary labels are removed

ilslv added 11 commits March 1, 2022 09:26
# Conflicts:
#	integration_tests/juniper_tests/src/codegen/mod.rs
#	integration_tests/juniper_tests/src/codegen/scalar_attr_derive_input.rs
#	integration_tests/juniper_tests/src/codegen/scalar_attr_type_alias.rs
#	integration_tests/juniper_tests/src/codegen/scalar_value_transparent.rs
#	integration_tests/juniper_tests/src/custom_scalar.rs
#	juniper/CHANGELOG.md
#	juniper/src/lib.rs
#	juniper/src/types/scalars.rs
#	juniper/src/value/scalar.rs
#	juniper_codegen/src/derive_scalar_value.rs
#	juniper_codegen/src/graphql_scalar/attr.rs
#	juniper_codegen/src/graphql_scalar/derive.rs
#	juniper_codegen/src/graphql_scalar/mod.rs
#	juniper_codegen/src/lib.rs
#	juniper_codegen/src/result.rs
# Conflicts:
#	juniper/src/integrations/chrono.rs
#	juniper/src/integrations/chrono_tz.rs
# Conflicts:
#	integration_tests/codegen_fail/fail/scalar_value/missing_methods.stderr
# Conflicts:
#	integration_tests/codegen_fail/fail/scalar_value/missing_attributes.rs
#	integration_tests/codegen_fail/fail/scalar_value/missing_attributes.stderr
#	integration_tests/codegen_fail/fail/scalar_value/multiple_named_fields.rs
#	integration_tests/codegen_fail/fail/scalar_value/multiple_unnamed_fields.rs
#	integration_tests/juniper_tests/src/custom_scalar.rs
#	juniper/CHANGELOG.md
#	juniper/src/integrations/chrono.rs
#	juniper/src/integrations/chrono_tz.rs
#	juniper/src/value/scalar.rs
#	juniper_codegen/src/lib.rs
@ilslv
Copy link
Member Author

ilslv commented Mar 10, 2022

FCM

Implement `#[derive(GraphQLInterface)]` to use structs as GraphQL interfaces (#1026)

- support `#[graphql_interface]` on structs

@ilslv ilslv changed the title Draft: Allow #[derive(GraphQLInterface)] and #[graphql_interface] on structs Allow #[derive(GraphQLInterface)] and #[graphql_interface] on structs Mar 10, 2022
@ilslv ilslv marked this pull request as ready for review March 10, 2022 12:33
@ilslv ilslv requested a review from tyranron March 10, 2022 12:45
Copy link
Member

@tyranron tyranron left a comment

Choose a reason for hiding this comment

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

@ilslv overall good, but requires a little bit of polishing though.

@@ -0,0 +1,2498 @@
//! Tests for `#[derive(GraphQLInterface)]` macro.

#![allow(dead_code)]
Copy link
Member

Choose a reason for hiding this comment

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

We better place this in codegen dirrectly, as it may appear in the user code frequently.


Ok(quote! {
#[allow(dead_code)]
#ast
Copy link
Member

Choose a reason for hiding this comment

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

Oh, right. You're doing this already. The issue is that we cannot place it with the proc_macro_derive.

Maybe we can generate some dummy global const stuff using type-asserting the struct or something like this instead?

"trait method should have a shared reference receiver `&self`",
);
fn err_unnamed_field<T, S: Spanned>(span: &S) -> Option<T> {
ERR.emit_custom(span.span(), "expected named struct field");
Copy link
Member

Choose a reason for hiding this comment

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

I don't see this case covered in codegen failure tests. Please, add.

.unwrap_or_else(|| format_ident!("{}Value", struct_ident.to_string()));
let enum_ident = attr.r#enum.as_ref().map_or_else(
|| format_ident!("{}ValueEnum", struct_ident.to_string()),
|c| format_ident!("{}Enum", c.inner().to_string()),
Copy link
Member

Choose a reason for hiding this comment

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

This code has become quite repetitive, so can be DRYed and factored out into a separate function for reusing.


/// Emits "expected named struct field" [`syn::Error`] pointing to the given
/// `span`.
fn err_unnamed_field<T, S: Spanned>(span: &S) -> Option<T> {
Copy link
Member

Choose a reason for hiding this comment

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

Meh... do not duplicate code. Just reuse the one from a neigbour module.

@ilslv
Copy link
Member Author

ilslv commented Mar 25, 2022

@tyranron CI build on nightly fails because of briansmith/ring#1469 and from what I can tell this is a bug in recent nightly version and how it handles doc comments inside decl macros briansmith/ring#1470

@ilslv ilslv requested a review from tyranron March 29, 2022 07:51
# Conflicts:
#	juniper_codegen/src/common/parse/mod.rs
@tyranron tyranron merged commit d0b56f9 into master Apr 1, 2022
@tyranron tyranron deleted the derive-interface-on-structs branch April 1, 2022 18:10
@tyranron tyranron added this to the 0.16.0 milestone Apr 13, 2022
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 k::api Related to API (application interface) semver::breaking Breaking change in terms of SemVer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants