【问题标题】:using graphql and simple express nodejs setup caussing bugs and errors使用 graphql 和简单的 express node js 设置导致错误和错误
【发布时间】:2020-12-03 21:37:18
【问题描述】:

我正在按照本教程构建一个简单的 graphql express 设置:: https://blog.bitsrc.io/how-to-build-a-note-taking-app-with-graphql-and-react-part-1-of-2-febf1aeda091

但是会出现一些错误:

我的文件是::

schema.js

const { makeExecutableSchema } = require('graphql-tools');
const { resolvers } = require('./resolvers');

const typeDefs = `
 type Note {
  _id: ID!
  title: String!,
  date: Date,
  content: String!
 }
scalar Date
type Query {
  allNotes: [Note]
 }`;

exports.schema = makeExecutableSchema({
    typeDefs,
    resolvers
});

resolvers.js

const Note = require("./models/note");

exports.resolvers = {
    Query : {
       async allNotes(){
           return await Note.find();
       }
    }
};

models/note.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const NoteSchema = new Schema({
    title : {
        type: String,
        required: true
    },
    content: {
        type: String,
        required: true
    },
    date: {
        type: Date,
        default: Date.now
    }
});
exports.Note = mongoose.model('Note', NoteSchema);

app.js

const express = require("express");
const mongoose = require("mongoose");
const {graphlHTTP} = require("express-graphql");
const {schema} = require("./schema");

mongoose.connect("mongodb+srv://hadoop:hadoop123@ecomdb-hcm1m.mongodb.net/nodeapi2?retryWrites=true&w=majority", {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true    
}).then(() => console.log('DB Connected'))

mongoose.connection.on('error', err => {
    console.log(`DB connection error: ${err.message}`)
});

const app = express();
const PORT = 4300;

app.get("/", (req, res) => {
  res.json({
    message: "Notetaking API v1"
  });
});

app.use(
    "/graphql",
    graphlHTTP({
      schema: schema,
      graphiql: true
    })
);

app.listen(PORT, () => {
  console.log(`Server is listening on PORT ${PORT}`);
});

================================================ ===========

一切看起来都很好,但仍然出现“TypeError:graphlHTTP 不是函数”错误。 我附上截图。请建议如何解决。

【问题讨论】:

    标签: node.js mongodb graphql


    【解决方案1】:

    我认为您在 app.js 中有错字。您需要导入 graphqlHTTP 而不是 graphlHTTP 并使用相同的。

    const {graphqlHTTP} = require("express-graphql");

    这里是 npm pacakge 链接。 https://github.com/graphql/express-graphql

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 2018-01-28
      • 2018-08-01
      • 2011-07-17
      • 2018-05-31
      • 2018-10-21
      • 2019-02-26
      相关资源
      最近更新 更多