i have some problem in some query data returning null or undefined from schema[typedefs]
after adding comment, this data returning null or undefined in subscriptions…
my subscription
Subscription: {
postCreated: {
subscribe: () => {
return pubsub.asyncIterator(POST_CREATED);
},
resolve: (payload) => {
return payload;
},
},
},
my typedefs
type Subscription{
postCreated: Post
}
type Post{
id: ID!
likes: [LikedPost]!
comments: [Comment]!
createdAt: Date
published: Boolean
user: User!
}
my post mutations
postCreate: async (
_: any,
{ post }: PostArgs,
{ db, userInfo }: Context
): Promise<PostPayloadType> => {
const { content } = post;
if (!userInfo) {
return {
userErrors: [
{
message: "Login or Signup to create a post",
},
],
post: null,
};
}
if (!content) {
return {
userErrors: [
{
message: "You must provide a content to create the post",
},
],
post: null,
};
}
const postCreated = await db.post.create({
data: {
content,
// author id must be dinamic data
authorId: userInfo.userId,
published: true,
},
});
pubsub.publish("POST_CREATED", postCreated);
return {
userErrors: [],
post: postCreated,
};
},
server.ts
import { typeDefs } from "./graphql/schema";
import { AllResolvers } from "./services/resolvers/resolvers";
import * as AllTypes from "./services/types/index";
import { getUserFromToken } from "./utility/getUserFromToken";
import { createServer } from "http";
import { execute, subscribe } from "graphql";
import { SubscriptionServer } from "subscriptions-transport-ws";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { ApolloServer } from "apollo-server-express";
import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";
import { prisma } from "./lib/prisma";
import { Context } from "@interface/context";
import express from "express";
export const db = prisma;
(async function () {
const app = express();
const httpServer = createServer(app);
const schema = makeExecutableSchema({
typeDefs,
resolvers: {
...AllResolvers,
...AllTypes,
},
});
const subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
onConnect() {
console.log("Subscription Server ready!");
},
onDisconnect() {
console.log("Subscription Server disconnected!");
},
},
{ server: httpServer, path: "/graphql" }
);
const server = new ApolloServer({
schema,
context: async ({ req }): Promise<Context> => {
// get user token and user.id from jwt bearer token
const userInfo = getUserFromToken(req.headers.authorization as string);
return {
db,
userInfo,
};
},
plugins: [
{
async serverWillStart() {
return {
async drainServer() {
subscriptionServer.close();
},
};
},
},
ApolloServerPluginLandingPageGraphQLPlayground(),
// ApolloServerPluginLandingPageDisabled(),
],
introspection: true,
});
await server.start();
server.applyMiddleware({ app });
const PORT = process.env.PORT || 4000;
httpServer.listen(PORT, () =>
console.log(`🚀 Server is now running on http://localhost:${PORT}/graphql`)
);
})();
Any solutions?