Leticia Esperon
Leticia Esperon

Reputation: 2841

Rails and graphql casing convention

I am starting to dive into graphql, building my first API with Rails. I have tons of questions regarding standards and conventions.

For example, I noticed the gem graphql by default requires the fields to be passed in CammelCase, when actually when I used to build RESTful API in Rails I always received the arguments in snake_case.

So I was wondering, is CammelCase the convention for Graphql? Should I still use it in my API? All my tests would assert the responses are in snake case too, is that correct?

Upvotes: 1

Views: 1162

Answers (1)

Unixmonkey
Unixmonkey

Reputation: 18784

It's certainly up to you and your application, and some libraries will automatically convert to and from camelCase, but given the choice, the GraphQL Rules website highly recommended that you just commit to using camelCase for all GraphQL fields and arguments, then convert those to camel_case fields in Ruby where necessary.

From the GraphQL Rules website website:

Rules and recommendations mentioned here were the results of 3 years' experience of using GraphQL both on the frontend and backend sides. We also include the recommendations and experience of Caleb Meredith (PostGraphQL author, Facebook ex-employee) and Shopify engineers.

  1. Naming rules

    1.1. Use camelCase for GraphQL-fields and arguments.

    1.2. Use UpperCamelCase for GraphQL-types.

    1.3. Use CAPITALIZED_WITH_UNDERSCORES to name ENUM-types.

I wish a bit that I had followed these for some past projects, but hey, they are still running just fine though :), but there has been churn where occassonally we were faced with some inconsistencies and chose to convert some fields.

Upvotes: 1

Related Questions