【问题标题】:Error: Expected undefined to be a GraphQL schema错误:预期未定义为 GraphQL 架构
【发布时间】:2019-04-11 01:02:26
【问题描述】:

我收到一条错误消息,上面写着“错误:预期未定义为 GraphQL 架构”。请检查这是什么问题 当我移动到 localhost:3000/graphiql 时,它会显示上述错误。 也许我做错了,请任何人检查并尽可能帮助我。

我的服务器.js

const express = require ('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');


//importing the Schema
const Story = require('./models/Story');
const User = require('./models/Story');


//Bring in GraphQl-Express middleware
const { graphiqlExpress, graphqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');

const { resolvers } = require('./resolvers');
const { typeDefs } = require('./schema');

//create schema
const Schema = makeExecutableSchema({
    typeDefs,
    resolvers
})


require('dotenv').config({ path: 'variables.env' });
// connecting mongoose to database
mongoose
.connect(process.env.MONGO_URI)
.then(()=> console.log('DB Connected'))
.catch(err => console.log(err));

//initializing express
const app = express();

//create GraphiQl application
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql'}))

//connect schemas with GraphQl
app.use('/graphql',bodyParser.json(), graphqlExpress({
    Schema,
    context:{
        Story,
        User
    }
}))

const PORT = process.env.PORT || 3000;

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

我的 Schema.js

exports.typeDefs = `


type Story {
    name: String!
    category: String!
    description: String!
    instructions: String!
    createDate: String
    likes: Int
    username: String
}



type User {
    username: String! @unique
    password: String!
    email: String!
    joinDate: String
    favorites: [Story]
}

type Query {
    getAllStories: [Story]
}


`;

我的 Resolvers.js

exports.resolvers= {

    Query:{
        getAllStories: ()=> {}
    }

};

我的故事.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const StorySchema = new Schema({
    name: {
        type: String,
        required:true,
    },
    category:{
        type:String,
        required:true
    },
    description:{
        type:String,
        required:true
    },
    instructions:{
        type:String,
        required:true
    },
    createdDate:{
        type:Date,
        default: Date.now
    },
    likes:{
        type:Number,
        default:0
    },
    username:{
        type:String
    }

})

module.exports = mongoose.model('Story', StorySchema );

我的用户.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const UserSchema = new Schema({
    username:{
        type:String,
        required:true,
        unique:true
    },
    password:{
        type:String,
        required:true
    },
    email:{
        type:String,
        required:true
    },
    joinDate:{
        type:Date,
        default:Date.now
    },
    favorites:{
        type:[Schema.Types.ObjectId],
        refs:'Story'
    }
})

module.exports = mongoose.model('User', UserSchema);

【问题讨论】:

    标签: javascript node.js reactjs api graphql


    【解决方案1】:

    您没有将架构传递给/graphql 端点的配置。属性名称在 javascript 中区分大小写。改变这个:

    app.use('/graphql',bodyParser.json(), graphqlExpress({
      Schema,
      context:{
        Story,
        User
      }
    }))
    

    到这里:

    app.use('/graphql',bodyParser.json(), graphqlExpress({
      schema: Schema,
      context:{
        Story,
        User
      }
    }))
    

    或将变量设为小写,然后执行:

    app.use('/graphql',bodyParser.json(), graphqlExpress({
      schema,
      context:{
        Story,
        User
      }
    }))
    

    【讨论】:

      【解决方案2】:

      2022 年 2 月更新:

      我将 graphql 降级为 15.x 然后问题就解决了:

      npm install graphql@15.8.0
      

      如果您不介意具体版本

      npm install graphql@15
      

      如果你使用“yarn”

      yarn add graphql@15.8.0
      

      如果您不介意具体版本

      yarn add graphql@15
      

      【讨论】:

        【解决方案3】:

        我使用最新版本的 graphql 和 @nestjs/graphql :"9.1.2" 版本遇到了这个问题。

        将版本降级到 15.8.0 并开始工作。

        npm i graphql@15.8.0

        【讨论】:

          猜你喜欢
          • 2020-06-01
          • 2018-12-10
          • 1970-01-01
          • 2021-12-23
          • 2021-12-15
          • 2021-03-17
          • 2020-11-26
          • 2017-06-12
          • 2019-05-15
          相关资源
          最近更新 更多