Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Dropdown Fields Returning Error #40

Closed
edolyne opened this issue Dec 21, 2017 · 8 comments
Closed

Dropdown Fields Returning Error #40

edolyne opened this issue Dec 21, 2017 · 8 comments

Comments

@edolyne
Copy link

edolyne commented Dec 21, 2017

When querying a dropdown field, the following error is returned:

"message": "Expected a value of type \"BackgroundColorEnum\" but received: \"background--primary\"",

"message": "Expected a value of type \"ReduceRemoveSpacingEnum\" but received: \"reduceSpacingTopBottom\"",


Craft & CraftQL versions:

Craft - RC3
CraftQL - dev-master

@markhuot
Copy link
Owner

Hrm, looks like I need to do some protection on the dropdown values. GraphQL has much more strict rules than Craft does, which is causing this.

@markhuot
Copy link
Owner

Okay, 1.0.0-beta.24 should fix this issue.

@edolyne
Copy link
Author

edolyne commented Dec 21, 2017

@markhuot Looks like it's returning values now, but the values returned don't match the value from the field.

example:

reduceSpacingTopBottom returns as reducespacingtopbottom
background--primary returns as backgroundPrimary

@markhuot
Copy link
Owner

background--primary isn't a valid ENUM value so I can't return that, as is… hrm. Maybe an Enum isn't the way to go for drop-downs…?

Not sure why reduceSpacingTopBottom is losing the casing, though, I'll look in to that.

@markhuot
Copy link
Owner

1.0.0-beta.25 fixes the casing issue you highlighted.

I'm not sure what to do about the hyphen issue, though. GraphQL won't allow hyphens in enum values. I could just have it return a string, but then I wouldn't be able to validate it on mutation/input. E.g.,

{
  upsertPost(
    title: "foo"
    dropdownField: "literally, anything, could be here"
  ) {
    id
  }
}

With enums we have some protection,

{
  upsertPost(
    title: "foo"
    dropdownField: someValidGraphQLEnum #anything else would fail before data even gets to Craft
  ) {
    id
  }
}

I'll need to think on this a bit… I'm open to any ideas you may have.

@edolyne
Copy link
Author

edolyne commented Dec 22, 2017

@markhuot Thanks for the quick fix. I appreciate the ability to validate the return type and then handle the variable in code appropriately. This might be an edge case that I was using the dropdown field to insert the value into the className.

@markhuot
Copy link
Owner

Thanks @edolyne. I'm going to close this ticket because we've fixed the incorrect casing on valid values.

One note on the invalid values: you can get the label of the input, if that helps like this,

{
  entries(id:2){
    ...on Stories{
      id
      title
      socialLinks #enum field, returns listOf enum values
    }
  }
  __type(name:"SocialLinksEnum"){ #the enum field I'm curious about
    enumValues{
      name # the value of the enum, maybe `myFieldValue`
      description # the label of the enum, maybe `My Field Value`
    }
  }
}

I wish there was some way that I could return the raw value inside enumValues but GraphQL doesn't provide any way to extend that with native enum's.

@markhuot
Copy link
Owner

At some point in the future I may consider subclassing the Enum type, allowing us to return the raw value. I've noted this in the roadmap.

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

No branches or pull requests

2 participants