使用express graphql、graphql、graphql订阅和graphql订阅ws在GraphiQL上运行订阅

使用express graphql、graphql、graphql订阅和graphql订阅ws在GraphiQL上运行订阅,graphql,graphql-js,express-graphql,graphql-subscriptions,Graphql,Graphql Js,Express Graphql,Graphql Subscriptions,我对GraphQL相当陌生,目前正在通过在前端使用React制作一个测验应用程序来熟悉自己 目前,我正忙于我的后端。在成功设置查询和变异之后,我发现订阅很难正常工作。使用GraphiQL时,我得到的是null作为输出,而不是“您的订阅数据将显示在此处…” 用于添加测验的查询和变异工作 我的服务器的入口点,app.js: 下面是模式定义,schema.js: 我环顾四周,但是我没有找到一种适合这种网站的方法。我知道这听起来可能是个简单的问题。任何帮助都将不胜感激 你找到解决办法了吗?嗨,@j0k

我对GraphQL相当陌生,目前正在通过在前端使用React制作一个测验应用程序来熟悉自己

目前,我正忙于我的后端。在成功设置查询和变异之后,我发现订阅很难正常工作。使用GraphiQL时,我得到的是
null
作为输出,而不是“您的订阅数据将显示在此处…”

用于添加测验的查询和变异工作

我的服务器的入口点,app.js

下面是模式定义,schema.js


我环顾四周,但是我没有找到一种适合这种网站的方法。我知道这听起来可能是个简单的问题。任何帮助都将不胜感激

你找到解决办法了吗?嗨,@j0k。不幸的是没有。我已决定不将其用于此应用程序。将来,我想我会在服务器端和客户端都使用Apollo。同时,如果你发现什么,请告诉我。
const express = require("express");
const mongoose = require("mongoose");
const { graphqlHTTP } = require("express-graphql");
const schema = require("./graphql/schema");
const cors = require("cors");

const port = 4000;

//Subscriptions 
const { createServer } = require("http");
const { SubscriptionServer } = require("subscriptions-transport-ws");
const { execute, subscribe } = require("graphql");

const subscriptionsEndpoint = `ws://localhost:${port}/subscriptions`;

const app = express();
app.use(cors());

mongoose.connect("mongodb://localhost/quizify", {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false
});

mongoose.connection.once("open", () => console.log("connected to database"));

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

const webServer = createServer(app);

webServer.listen(port, () => {
    console.log(`GraphQL is now running on http://localhost:${port}`);

    //Set up the WebSocket for handling GraphQL subscriptions. 
    new SubscriptionServer({
        execute,
        subscribe,
        schema
    }, {
        server: webServer,
        path: '/subscriptions',
    });
});
const { graphqlHTTP } = require("express-graphql");
const graphql = require("graphql");
const { PubSub } = require("graphql-subscriptions");

const pubsub = new PubSub();

//Import of Mongoose Schemas: 
const Quiz = require("../models/quiz");

const {
    GraphQLObjectType,
    GraphQLList,
    GraphQLSchema,
    GraphQLNonNull,
    GraphQLID,
    GraphQLString,
    GraphQLBoolean
} = graphql;

const QuizType = new GraphQLObjectType({
    name: "Quiz",
    fields: () => ({
        id: { type: GraphQLID },
        title: { type: GraphQLString },
        questions: {
            type: new GraphQLList(QuestionType),
            resolve(parent, args) {
                return Question.find({ quizId: parent.id });
            }
        },
        creator: {
            type: UserType,
            resolve(parent, args) {
                return User.findById(parent.creatorId);
            }
        }
    })
});

const NEW_QUIZ_ADDED = "new_quiz_added";

const Subscription = new GraphQLObjectType({
    name: "Subscription",
    fields: {
        quizAdded: {
            type: QuizType,
            subscribe: () => {
                pubsub.asyncIterator(NEW_QUIZ_ADDED);
            },
        },
    }
});

const Mutation = new GraphQLObjectType({
    name: "Mutation",
    fields: {
        createQuiz: {
            type: QuizType,
            args: {
                title: { type: new GraphQLNonNull(GraphQLString) },
                creatorId: { type: new GraphQLNonNull(GraphQLID) }
            },
            resolve(parent, args) {
                const newQuiz = new Quiz({ //Quiz imported from Mongoose schema. 
                    title: args.title,
                    creatorId: args.creatorId,
                });
                pubsub.publish(NEW_QUIZ_ADDED, { quizAdded }); //NEW_QUIZ_ADDED - a constant defined above for easier referencing. 
                return newQuiz.save();
            }
        },
    },
});

module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation: Mutation,
    subscription: Subscription,
});