8

I saw this statement in Graphql directive definition:

const { resolve = defaultFieldResolver } = field;

I know the part const { resolve } = field; means getting resolve property of the field object out and storing it in a local variable resolve. But what I'm coming across for the first time is the = defaultFieldResolver part. What does the equal sign do here? I've done a thorough google search but maybe I'm not aware of the right keyword to search or this is probably something new.

Here's the link of the article where I saw this.

Thanks a bunch.

Ivar
  • 6,138
  • 12
  • 49
  • 61

1 Answers1

11

That means if field contains a resolve property, extract it:

const defaultFieldResolver = 'defaultFieldResolver';

const field = { resolve: 'resolve' };

const { resolve = defaultFieldResolver } = field;

console.log(resolve);

If field doesn't contain a resolve property, assign defaultFieldResolver to the resolve variable instead.

const defaultFieldResolver = 'defaultFieldResolver';

const field = {};

const { resolve = defaultFieldResolver } = field;

console.log(resolve);
Guerric P
  • 30,447
  • 6
  • 48
  • 86