Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
GraphQL - problem with the code from the tutorial page, it pops up an error "Expected undefined to be a GraphQL schema."

I try to learn GraphQL. I wanted to try out the code that is in the Getting Started With GraphQL.js section in the graphql.org. I created a server.js file like this page says: https://graphql.org/graphql-js/

I followed instructions like:

npm init
npm install graphql --save

The package.json file looks like this:

{
  "name": "graphql",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "graphql": "^16.0.1"
  }
}

The server.js file I created looks exactly like the one from the page (it was copied):

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);
});

But after using the node server.js command (as written on the page) in the terminal, I get this error:

node_modules\graphql\type\schema.js:35
    throw new Error(
          ^

Error: Expected undefined to be a GraphQL schema.
    at assertSchema (F:\GraphQL\node_modules\graphql\type\schema.js:35:11)
    at validateSchema (F:\GraphQL\node_modules\graphql\type\validate.js:34:28)
    at graphqlImpl (F:\GraphQL\node_modules\graphql\graphql.js:52:64)
    at F:\GraphQL\node_modules\graphql\graphql.js:21:43
    at new Promise (<anonymous>)
    at graphql (F:\GraphQL\node_modules\graphql\graphql.js:21:10)
    at Object.<anonymous> (F:\GraphQL\server.js:18:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)

Does anyone know what the problem is? Maybe I am missing some package? Or the code given on the page is wrong? Please help me, thank you in advance for any answer.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda