0

I follow the tutorial from GraphQl https://graphql.org/graphql-js/

var { graphql, buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

// The root provides a resolver function for each API endpoint
var root = {
  hello: () => {
    return 'Hello world!';
  },
};

// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
  console.log(response);
});

When I run it, I expect to get

{ data: { hello: 'Hello world!' } }

However, instead I get

{ data: [Object: null prototype] { hello: 'Hello world!' } }

Why is that?

Elye
  • 53,639
  • 54
  • 212
  • 474

1 Answers1

2

That's just how console.log shows objects that have null as their prototype instead of Object.prototype.

Compare

console.log({}) // {}

or

console.log(Object.create()) // {}

with

console.log(Object.create(null)) // [Object: null prototype] {}

Object.create(null) initializes an object that doesn't inherit any properties or methods from Object. There's a few reasons to do that.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183