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

expected identifier, found keyword Error in derive(Interface) with field(name = "type",..) #1140

Closed
tacogips opened this issue Nov 10, 2022 · 1 comment
Labels
question Further information is requested

Comments

@tacogips
Copy link

tacogips commented Nov 10, 2022

I have a schema with interface which has field name type.

enum UserType {
  Free
  Payed
}

interface User {
  id: ID!
  type: UserType
}

type Me implements User {
  id: ID!
  type: UserType
  email: String
}

and the rust definition would be as below.

#[derive(Enum, Copy, Clone, Debug, Eq, PartialEq)]
pub enum UserType {
    Free,
    Payed,
}

#[derive(Interface)]
#[graphql(
    field(name = "id", type = "ID"),
    field(name = "type", type = "Option <UserType>")
)]
#[derive(Debug, Clone)]
pub enum User {
    Friend(Friend),
    Me(Me),
}

#[derive(Debug, Clone)]
pub struct Me {
    pub id: ID,
    pub email: Option<String>,
}

#[Object]
impl Me {
    pub async fn id(&self) -> ID {
        self.id.clone()
    }
    pub async fn r#type(&self, ctx: &Context<'_>) -> Result<Option<UserType>> {
        unimplemented!()
    }
    pub async fn email(&self) -> Option<String> {
        self.email.clone()
    }
}

Then I got a error in the derive macro.

error: expected identifier, found keyword `type`
 --> examples/some/main.rs:8:10
  |
8 | #[derive(Interface)]
  |          ^^^^^^^^^ expected identifier, found keyword
  |
  = note: this error originates in the derive macro `Interface` (in Nightly builds, run with -Z macro-backtrace for more info)
help: escape `type` to use it as an identifier

I'd say it caused by calling method which name is same as rust keyword like self.type() in the token stream.

changing the field name to r#type didn't solve it.

 field(name = "r#type", type = "Option <UserType>")
...
8 | #[derive(Interface)]
  |          ^^^^^^^^^
  |
  = help: message: `"r#type"` is not a valid identifier

Is there anyway to workaround this issue?

versions

async-graphql:4.0.15

@tacogips tacogips added the question Further information is requested label Nov 10, 2022
@sunli829
Copy link
Collaborator

Fixed in master branch 🙂

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

No branches or pull requests

2 participants