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

Interface according to docs is failing to compile #1104

Closed
Anderssorby opened this issue Sep 12, 2022 · 2 comments
Closed

Interface according to docs is failing to compile #1104

Anderssorby opened this issue Sep 12, 2022 · 2 comments
Assignees
Labels
Milestone

Comments

@Anderssorby
Copy link

I'm having trouble implementing the cursor connections based pagination specification. https://relay.dev/graphql/connections.htm
My Node implementation is like this:

/// A node in a pagination query.
#[graphql_interface(for = [Company], scalar = juniper::DefaultScalarValue)]
pub trait Node {
  fn id(&self) -> String;
}

And company is like this:

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Company {
  pub uuid: Uuid,
  pub name: String,
  #[serde(default)]
  pub users: Vec<ClientUserNoCompany>,
}

#[graphql_object(impl = [NodeValue], description = "A generic company")]
impl Company {
  pub fn id(&self) -> String { self.uuid.to_string() }
}

But I'm getting errors like this

 #[graphql_object(impl = [NodeValue], description = "A generic company")]
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `GraphQLInterface<__S>` is not implemented for `NodeValueEnum<Company>`

Why is this happening? Note: I'm using the master branch

@tyranron tyranron self-assigned this Sep 12, 2022
@tyranron tyranron added this to the 0.16.0 milestone Sep 12, 2022
@tyranron
Copy link
Member

@Anderssorby the Book on the master branch is quite outdated, sorry for that. We're not going to update it now, because there are some more fundamental changes to happen. We're going to update it right before release.

But, the code docs in juniper_codegen crate should be fine and correct.

The problem with your example is in scalar = juniper::DefaultScalarValue attribute argument on the Node trait. By default, the derived implementations are abstracted over ScalarValue types via type parameter __S: ScalarValue, and you define the Company GraphQL object this way. On the other hand, the Node GraphQL interface is derived for juniper::DefaultScalarValue usage only, not for any __S: ScalarValue. That's why compilation fails. So:

  1. either remove scalar = juniper::DefaultScalarValue attribute argument from the Node interface definition;
  2. or add scalar = juniper::DefaultScalarValue attribute argument to the Company GraphQL object definition.

I'd recommend the option 1.

I'm trying to rework the whole traits machinery in #1072, so it will be much more flexible and polymorphic, without such gotchas. However, it needs quite a time to land.

@Anderssorby
Copy link
Author

Thanks. Maybe there should be an explicit check and warning for this specific error.

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

No branches or pull requests

2 participants