0

I'm using react-select as a 'multi-select' in a react-app to set multiple music genres. This results in an array of objects as the value for that input. I'm using AWS Amplify / DynamoDB.

I'm not sure if this is an issue with my schema.graphql (below), how I'm handling / mishandling the data, or just my general coding deficits, but the data gets sent like this:

[{label='Afrobeat', value='Afrobeat'}, {label='Bikutsi', value='Bikutsi'}, {label='Benga', value='Benga'}]

And returns like this (single quotes stripped), double quotes added around each node):

["{label=Afrobeat, value=Afrobeat}", "{label=Bikutsi, value=Bikutsi}", "{label=Benga, value=Benga}"]

react-select can't process the data like that but I can't imagine I would have to do some tricky client-side handling to make it work. Any help would be graciously appreciated.

My schema.graphql for this Song type is:

type Song @model {
  album: Album @connection(name: "AlbumSongs")
  composer: String
  description: String
  duration: String
  genre: [String!]! // is this the correct value for an array?
  id: ID!
  instruments: String
  tags: [String!]!
  title: String!
}

1 Answer 1

1

Need to have another type GenreOptions to make it return as an array of objects instead of array of strings.

Something like:

type GenreOptions {
  label: String!
  value: String! // also another option is to have an enum type
}

type Song @model {
  genre: [GenreOptions!]!
}

Then the query would look like:

Song {
  genre {
    label
    value
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you kindly. I have over a thousand genres... how would I 'enum' those in the GenreValues object? (presently I'm just pulling them from a data.js file in my app).
No problem. Another alternative for enum is just value: String!.
Awesome. So to be clear, like this: enum GenreValues { value: String! }? (SV Code is coloring it weird so I'm wondering if I have it right).
@KirkRoss I've updated the code. No need for enum if you'll be using string.
Gigathanks. BTW... trivia... if you happen to have watched, Silicon Valley my brother plays Gavin Belson on it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.