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

Deriving instance FromValue - guideline for using underscore in Record field names #201

Open
EdmundsEcho opened this issue Sep 2, 2018 · 2 comments

Comments

@EdmundsEcho
Copy link

EdmundsEcho commented Sep 2, 2018

First just a few words of mention to thank everyone for their work on this library. Even at this somewhat early stage of development, the library has proved useful and productive - I can't imagine hydrating our App's React-based UI any other way! Also, congratulations to everyone for being added to the "State of the Haskell Ecosystem" maintained by Gabriel Gonzalez among other leading contributors in the Haskell community.

To the question: There is a Haskell norm using the Lens library to name the record fields with a prefix underscore (e.g., _myField). When the library derives the required Lens instances it will then name the lenses without the prefix (my lens for that field is automagically referenced as myField). Nice.

In the graphql-api we can derive FromValue and HasAnnotatedInputType instances for our Haskell data record types we intend to use as GraphQL ObjectInputType. The library has taken on the same convention. However, there are two ways/places to reference the user input where the benefit of opting-in to the convention is different:

  1. Referencing the fields in a GraphQL resolver (like what is done in the example for ObjectInput; lines 31-34)
  2. Referencing the fields in Haskell e.g., to instantiate a Haskell type, or otherwise mutate the Haskell state (non-GraphQL division of the App if you will). In this case, the returned output from GraphQL (the "view") might be a GraphQL view (Object type) of a different type, i.e., not the ObjectInputType.

In the first case, I can see the benefit of using the lower case prefix. In the second, I don't other than to avoid name clashes, but that's not the main point.

The question: As a user (not the library designer), what is the downside of not using the underscore prefix if I'm in the second use case scenario? Also, what names does the Generic derived instance generate when not using the underscore prefix convention?

Thank you again for your input. I hope some of these questions serve a broader audience, and might provide fodder for the next generation of documentation.

- E

@jml
Copy link
Collaborator

jml commented Sep 4, 2018

Thanks for the kind words, @EdmundsEcho.

I'm not sure if I understand the question. Could you perhaps provide some concrete, minimal examples?

@EdmundsEcho
Copy link
Author

Sure,

Note the use of the _ prefix

data DogStuff = DogStuff { _toy :: Text, _likesTreats :: Bool } deriving (Show, Generic)

When do we reference this without the underscore? When is it best practice to use the underscore?

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

No branches or pull requests

2 participants