0

index.js

const {GraphQLServer, GrphQLServer, PubSub } = require('graphql-yoga');
const db = require('./db');
const Query = require('./resolvers/Query');
const Mutation = require('./resolvers/Mutation');
const subscription = require('./resolvers/Substription')

const pubsub = new PubSub();



const typeDefs=`

type Query{

me:User!
signlevalue(name:String):String
sum(marks:[Int]):Int!
user:[User]

}




type Mutation{
createUser(id:ID,name:String,email:String):[User]
}


type User{
id:ID
name:String
email:String
age:Int
}


type Subscription{
    
    count:Int
}


`


const resolvers={
    Query,
    Mutation,
    subscription

  

}

const server= new GraphQLServer({
    typeDefs,
    resolvers,
    context:{
        db,
        pubsub
    }
})

server.start(()=>{
    console.log('servr is running on port 4000');
})

Substription.js

  const Subscription={
    count:{
        subscribe(parent,args,{pubsub},info){
            let count=0;
            setInterval(() => {
                count++;
                pubsub.publish('count',{
                    count:count
                })
                
            }, 1000);
            return pubsub.asynsIterateor('count')
            
        }

    }
  
   
}


module.exports = Subscription;

包.json

{
  "name": "sql",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon src/index.js --ext js,graphql --exec babel-node",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "express": "^4.17.1",
    "express-graphql": "^0.11.0",
    "graphql": "^15.3.0",
    "graphql-yoga": "^1.18.3",
    "lodash": "^4.17.20"
  }
}

我是graphQL词的大人物,我不知道这里有什么问题,因为我已经在模式中定义了订阅,但它仍然给我在解析器中定义的错误“订阅”,但不是在模式中我已经搜索过它但没有找到任何解决方案跟随udemy的安德鲁米德课程

4

1 回答 1

0

您的导入名称是“subscription”(small s),而在您的类型定义中,您使用的是“Subscription”(large s)

用这个

  const Subscription = require('./resolvers/Substription')
于 2020-11-08T06:26:57.633 回答