I’ve been using graphql with meteor for awhile and decided to try subscriptions today. I’ve setup my server (or so I thought).
For some reason, when I try to include my subscription server, my client can no longer find the endpoint for ‘localhost:/3000/graphql’
I have a file for firing up my server which has been working as expected:
import { createApolloServer } from 'meteor/apollo'; // meteo package
import { makeExecutableSchema } from 'graphql-tools';
import { typeDefs, resolvers } from '/imports/api/schema'; // my schema
import {loadSchema, getSchema} from 'graphql-loader' //used for loading meteor-apollo-accounts into my schema?
import {initAccounts} from 'meteor/nicolaslopezj:apollo-accounts' //initialize accounts page
import cors from 'cors'; // need to config into my server
//set options
// Load all accounts related resolvers and type definitions into graphql-loader
initAccounts({});
// Load all your resolvers and type definitions into graphql-loader
loadSchema({typeDefs, resolvers});
// Gets all the resolvers and type definitions loaded in graphql-loader
export const schema = makeExecutableSchema(getSchema());
//create server
createApolloServer({ schema }, {
configServer: graphQLServer => graphQLServer.use(cors()),
});
when I try to add my subscription server, my client can’t find the graphql endpoint anymore.
import { createApolloServer } from 'meteor/apollo'; // meteo package
import { makeExecutableSchema } from 'graphql-tools';
import { typeDefs, resolvers } from '/imports/api/schema'; // my schema
import {loadSchema, getSchema} from 'graphql-loader' //used for loading meteor-apollo-accounts into my schema?
import {initAccounts} from 'meteor/nicolaslopezj:apollo-accounts' //initialize accounts page
import cors from 'cors'; // need to config into my server
import { subscriptionManager } from "./graphQL-subscriptions-config.js";
import { createApolloSubscriptionServer } from "./createApolloSubscriptionServer";
//set options
// Load all accounts related resolvers and type definitions into graphql-loader
initAccounts({});
// Load all your resolvers and type definitions into graphql-loader
loadSchema({typeDefs, resolvers});
// Gets all the resolvers and type definitions loaded in graphql-loader
export const schema = makeExecutableSchema(getSchema());
//create server
createApolloServer({ schema }, {
configServer: graphQLServer => graphQLServer.use(cors()),
});
// subscription server
createApolloSubscriptionServer({
subscriptionManager,
});
the sub manager:
// server/subscriptions.js
import { PubSub, SubscriptionManager } from 'graphql-subscriptions';
import { schema } from './graphQL-config'
import { Friends } from '/imports/api/Friend'
export const pubsub = new PubSub();
export const subscriptionManager = new SubscriptionManager({
schema,
pubsub,
setupFunctions: {
postAdded: (options, args) => ({
postAdded: {
filter: post => {
console.log(post.ownerId)
let myFriends = Friends.find({ownerId: args.userId}).fetch();
let myFriendIds = myFriends.map( item => item.friendId);
if (!myFriendIds || myFriendIds.length === 0) { return false }
console.log(myFriendIds.includes(post.ownerId))
myFriendIds.includes(post.ownerId)
}
},
}),
},
});
file that creates sub server:
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
const WS_URL = process.env.WS_URL || `localhost:3000`;
const wsUrlParts = WS_URL.split(':');
const WS_PORT = wsUrlParts[wsUrlParts.length - 1];
const SUBSCRIPTIONS_PATH = '/subscriptions';
Meteor.settings.public.WS_URL = process.env.WS_URL || `ws://localhost:3000`;
const createApolloSubscriptionServer = ({ subscriptionManager }) => {
const httpServer = createServer((request, response) => {
response.writeHead(404);
response.end();
});
console.log(WS_PORT)
httpServer.listen(WS_PORT, () => console.log(
`Websocket Server is now running on port ${WS_PORT}`
));
return new SubscriptionServer({ subscriptionManager }, {
server: httpServer,
path: `${Meteor.settings.public.WS_URL}/${SUBSCRIPTIONS_PATH}`
});
}
export { createApolloSubscriptionServer };
client:
//APOLLO SPECIFIC
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { meteorClientConfig } from 'meteor/apollo';
const client = new ApolloClient(meteorClientConfig());
export default client;