One weird trick to make your GraphQL-JS resolvers more concise

I was looking at some of my GraphQL resolver code, and sometimes I had some resolvers that looked like this, just to rename a field:

// Option 1
resolve: (obj) => obj.field_name
// Option 2
resolve: ({ field_name }) => field_name

This can come up if your SQL schema has snake_case and you want your GraphQL fields to be camelCase .

So what’s the trick? lodash, of course!

Check this out:

import { property } from 'lodash';
resolve: property('field_name') 

Now that looks quite semantic!

Bonus trick!

I know I said one weird trick, but why not two?

import { constant } from 'lodash';
resolve: constant(5)

Now, you can have resolvers that return constant values, for example if you’re debugging.

I hope I made your JavaScript GraphQL server 0.5% better today.