Search code examples
functional-programmingprisma-graphqlrescript

How to get away from Non-capitalized key value for Record construction?


I am using graphql, nexus-plugin-prisma, prisma to build a backend application using ReScript. The problem I face is that there are some columns starting with capital letter, and I want to set types for such schemas using records instead of objects. (to make use of pattern matching utility)

But ReScript prevents capitalized letters to appear as the very first character of a key of a record. Is there any way that I can somehow get away with this issue? Any help would be appreciated.


Solution

  • Usually when dealing with graphQL, the best way to circumvent this issue is to make use of graphQL aliasing feature:

    fragment Foo on foo {
      uncapitalizedAlias: CapitalizedName
    }
    

    edit: I don't know if the records you're trying to use are defined beforehand or generated by your GraphQL client, but you have other more general solutions when you want to bind to JS objects with capitalized names:

    1. you can make use bs.as to change the name of the field (works both in Ocaml/Reason and Rescript syntaxes):
    type myGQLrecord = {
      @bs.as("CapitalizedName")
      uncapitalizedName: string,
    }
    
    
    1. or you can directly use any identifier name you want for your field thanks to this feature of rescript syntax (works also for value identifiers):
    type myGQLrecord = {
      \"CapitalizedName": string
    }
    

    In my opinion, it makes it a bit more cumbersome to use though.