0

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?

Leticia Esperon
  • 2,499
  • 1
  • 18
  • 40
  • 1
    The Gem will juggle the casing for you in most cases. So when you build types, for example, you can define the fields as snake_case. The Gem will always assume that the client is using the GraphQl convention, which is camelCase. I would refer to the official GraphQL docs for that information: https://graphql.org/learn/schema/#type-system – TonyArra Apr 08 '20 at 19:29

1 Answers1

1

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.

Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • `then convert those to camel_case fields in Ruby where necessary.` Just to clarify, did you mean convert those to snake_case? fields in Ruby where necessary? – David Lee May 12 '21 at 14:42
  • 1
    @DavidLee Yes. The Graphql-Ruby gem will automatically convert Ruby's snake_case to GraphQL camelCase, for instance: `field :current_user, UserType, null: true` in Ruby, will generate as `currentUser` in your GraphQL schema. – Unixmonkey May 12 '21 at 14:54