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 error]: Message: String cannot represent value: { name: “Bar”, ordinal: 1 } #2093

Closed
stefan-jevtic opened this issue Aug 16, 2019 · 3 comments
Labels

Comments

@stefan-jevtic
Copy link

Hello folks,

I’m working on a project which hasn’t updated dependencies for a while. Today we did update and now I’m facing a problem with graphql. We have fields in our app which are enumerators and for that purpose, we are using enumify npm package. In type definition, I have this scenario e.g.

enum FooType {
    ${map(FooType.enumValues, 'name').join(' ')}
}
input FooInput {
    type: FooType
}
type Foo {
    type: String
}
type Query {
  getFoo(id: ID!) Foo
}

And when I query getFoo I’m getting this error: [GraphQL error]: Message: String cannot represent value: { name: “Bar”, ordinal: 1 }

This project setup worked in the previous version which was very old and after updating dependencies graphql 0.13.2 —> 14.4.2, graphql-tools 3.0.5 —> 4.0.5 and react-apollo 2.2.4 —> 3.0.1 I’m getting the error as I mentioned above.

Any types? Am I missing some additional configuration for this new version or I need to change the logic in resolvers and type definitions?

@IvanGoncharov
Copy link
Member

@stefan-jevtic If you use this enum only inside input types (as shown in your example) it means your client sending { name: “Bar”, ordinal: 1 } instead of "Bar" inside query variable.
Before 14.0.0 we missed proper validation for such cases so { name: “Bar”, ordinal: 1 } was silently converted in [object Object] string and pass into your resolver.
Since [object Object] is not something you should get as an argument we can't revert the previous behavior so ideally, you would fix your client app to sent correct values.

If it's not possible to fix your client you can convert enum FooType into scalar FootType that will disable all validations.

@stefan-jevtic
Copy link
Author

Well, it's not the problem while I'm inserting new Foo, it's a problem when I send a query to fetch Foos. My model in javascript looks something like this

class Foo implements IFoo {
    id: sting;
    name: string;
    ....
    type: FooType
}

where the fooType is enum implemented through enumify npm package, and resolver returns an instance of that model. So, in the version before update, this scenario worked like a charm. I'm hoping that you get a point what I want to say. My client does not send { name: “Bar”, ordinal: 1 } it sends a string that belongs to that enum defined in type definition and that case works well. Only is the problem while I'm fetching Foos. Do I need to change the type of field in my model to string if graphql cannot convert an object to a string?

@IvanGoncharov
Copy link
Member

Only is the problem while I'm fetching Foos.

@stefan-jevtic So it means you returning { name: “Bar”, ordinal: 1 } in your resolvers.
You should return just Bar instead of the entire object.

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