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

Automatic object derivation for complex types #231

Closed
meddulla opened this issue Apr 18, 2017 · 5 comments
Closed

Automatic object derivation for complex types #231

meddulla opened this issue Apr 18, 2017 · 5 comments
Labels

Comments

@meddulla
Copy link

Hi,

If I have a case class with complex types, e.g..

case class IndexComponentDb(id: String, name: String, weight: Double)
case class ShareClassMetaDb(
                             id: UUID,                             
                             countriesOfSale: Set[String],
                             status: String,
                             benchmarks: List[IndexComponentDb],
                             [...])

and then do automatic object derivation

val ShareClassType: ObjectType[ShareClassModel.type, ShareClassMetaDb] = {
    deriveObjectType[ShareClassModel.type, ShareClassMetaDb](
      ObjectTypeName("ShareClass"),
      ObjectTypeDescription("A share class."),
      ExcludeFields("id"))
  }

then these kinds of errors appear:

Can't find suitable GraphQL output type for Set[String], 
Can't find suitable GraphQL output type for List[IndexComponent] 

Once we have GraphQL types for the base types, in this case, String and IndexComponent, can't Sangria automatically derive List's and Set's of them?

I can define a GraphQL output type for Set[String] but it doesn't make much sense.. so I think I'm probably missing something.

So how does one provide an output type in these kinds of cases?

Thanks!

@OlegIlyenko
Copy link
Member

The List (in fact any Seq) is supported, but Set yet supported. I would suggest to use some Seq instead or use ReplaceField macro setting to transform a set into a sequence just for GraphQL type.

You will also need to define/import an implicit instance of OutputType[IndexComponentDb] somewhere in scope to get rid of the second compilation error.

@meddulla
Copy link
Author

meddulla commented Apr 20, 2017

Thank you, ReplaceField was what I needed 👍

Now another question :) How about maps? I know they don't make much sense in this context but if we were to implement an OutputType for Map[String, String] it would have to be something akin to raw json, similar to this https://gist.github.com/OlegIlyenko/5b96f4b54f656aac226d3c4bc33fd2a6 perhaps?

@OlegIlyenko Thank you so much for your help - sangria (and especially the macros) has been great!

@OlegIlyenko
Copy link
Member

Thank you for your kind words! 🙇

Using a raw JSON scalar can be an option, but I would discourage using it, especially considering that you have simple String values in the Map. I would suggest you to check out this issue: graphql/graphql-spec#101. I also commented there. I think there you can find several solutions on how to model it.

@meddulla
Copy link
Author

meddulla commented May 8, 2017

Sorry for the long delay but been on holidays. That link provided some useful clues on how to solve this, so thanks :)

@meddulla meddulla closed this as completed May 8, 2017
@kflorence
Copy link

Just ran into this. Might be nice to mention somewhere in the README a list of gotchas like this to save people some time.

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

3 participants