0

during an online course I found this syntax:

const graphql = require('graphql');
const{
     GraphQLObjectType
} = graphql;
...

My question is: What is that means the second part of the code? Is like import some property name from the graphql library? I checked on const definition, some other forums but I dind't find anything.

Thanks

  • [destructuring assignment](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). Equivalent to `const = graphql.GraphQLObjectType;` – dfsq May 03 '17 at 12:32

1 Answers1

2

This is an example of 'destructuring assignment', which allows you to easily extract parts of an object or array into a variable.

const { GraphQLObjectType } = graphql;

// is the same as

const GraphQLObjectType = graphql.GraphQLObjectType;

let obj = { a: 0, b: 1 };
let { a, b } = obj;

// is the same as

let obj = { a: 0, b: 1 };
let a = obj.a;
let b = obj.b;

var arr = [0, 1, 2];
var [ a, b ] = arr;

// is the same as

var arr = [0, 1, 2];
var a = arr[0];
arv b = arr[1];
Joe Clay
  • 26,622
  • 4
  • 68
  • 75