私はコンテキスト接続でそれを使用し、購読パラメータをApolloサーバーに追加しようとしましたが、それは機能しません。 Apollo Serverサブスクリプションを使用していますが、エラーがサーバー構成またはリゾルバ内にあるかどうかわかりません。クエリや突然変異に問題がありませんが、問題は購読です。
import express from 'express';
import { createServer } from 'http';
import { ApolloServer } from 'apollo-server-express';
import { typeDefs } from './data/schema';
import { resolvers } from './data/resolvers';
import cors from 'cors';
import jwt from 'jsonwebtoken';
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 4004;
const app = express();
app.use(bodyParser.json());
app.use(cors());
const server = new ApolloServer({
typeDefs,
resolvers,
context: async({req, connection}) => {
console.log("Context connection", connection)
const token = req.headers['authorization'];
if(connection){
return connection.context;
} else {
if(token !== "null"){
try{
//validate user in client.
const currentUser = await jwt.verify(token, process.env.SECRET);
//add user to request
req.currentUser = currentUser;
return {
currentUser
}
}catch(err){
return "";
}
}
}
},
subscriptions: {
path: "/subscriptions",
onConnect: async (connectionParams, webSocket, context) => {
console.log(`Subscription client connected using Apollo server's built-in SubscriptionServer.`)
},
onDisconnect: async (webSocket, context) => {
console.log(`Subscription client disconnected.`)
}
}
});
server.applyMiddleware({app});
const httpServer = createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen({ port: PORT }, () =>{
console.log(`???? Server ready at
http://localhost:${PORT}${server.graphqlPath}`)
console.log(`???? Subscriptions ready at
ws://localhost:${PORT}${server.subscriptionsPath}`)
})
_
mutation {
pushNotification(label:"My septh notification") {
label
}
}
_
query {
notifications {
label
}
}
_
subscription {
newNotification {
label
}
}
_
{
"error": {
"message": "Cannot read property 'headers' of undefined"
}
}
_
コンテキストコールバックのときは、JWTトークンを確認できます
server = new ApolloServer({
schema: schema ,
graphiql: true ,
context:({req, connection} )=>
if connection
token = connection.context["x-access-token"]
decoded = await LoginService.verify token #verify by jwt
if decoded == null
throw new Error("auth required")
return connection.context
headers = req.headers
token = headers["x-access-token"]
decoded = await LoginService.verify token #verify by jwt
return authed: decoded != null
})
_