Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal sign inside object destructuring curly braces

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.

like image 499
MuhammadAbdullah Avatar asked May 20 '26 23:05

MuhammadAbdullah


1 Answers

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);
like image 70
Guerric P Avatar answered May 22 '26 14:05

Guerric P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!