I’m updating my code to use apollo-server-express. My queries and mutations are working, but I don’t quite have subscriptions working yet.
Does someone here have server setup code they could post?
Here’s my current setup code:
Server
import { ApolloServer } from 'apollo-server-express';
import { createServer } from 'http';
import {WebApp} from 'meteor/webapp';
import {getUser} from 'meteor/apollo';
import express from 'express';
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import typeDefs from '/imports/apollo/schema';
import {getUserData, resolvers} from "./api/resolvers";
// https://www.apollographql.com/docs/apollo-server/data/subscriptions/#enabling-subscriptions
// Create the schema, which will be used separately by ApolloServer and
// the WebSocket server.
const schema = makeExecutableSchema({typeDefs, resolvers});
// Create an Express app and HTTP server; we will attach both the WebSocket
// server and the ApolloServer to this HTTP server.
const app = express();
const httpServer = createServer(app);
// Create our WebSocket server using the HTTP server we just set up.
const wsServer = new WebSocketServer({
server: httpServer,
path: '/graphql',
});
// Save the returned server's info so we can shutdown this server later
const serverCleanup = useServer({schema}, wsServer);
// Set up ApolloServer.
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({req}) => ({
user: await getUser(req.headers.authorization)
}),
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({httpServer}),
// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose();
},
};
},
},
],
});
export async function startApolloServer() {
await server.start();
const app = WebApp.connectHandlers;
server.applyMiddleware({
app,
cors: true
});
httpServer.listen(PORT, () => {
console.log(
`Server is now running on ${baseUrl}:${PORT}${server.graphqlPath}`,
);
})
}
Client
import React from 'react';
import { InMemoryCache, ApolloProvider, ApolloClient, ApolloLink } from '@apollo/client';
import { BatchHttpLink } from '@apollo/client/link/batch-http'
import { MeteorAccountsLink } from 'meteor/apollo'
// https://www.apollographql.com/docs/react/data/subscriptions/
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient } from 'graphql-ws';
const wsLink = new GraphQLWsLink(createClient({
url: 'ws://localhost:4000/subscriptions',
}));
const cache = new InMemoryCache();
const link = ApolloLink.from([
MeteorAccountsLink(),
new BatchHttpLink({
uri: '/graphql'
})
]);
const client = new ApolloClient({
uri: '/graphql',
cache,
link,
});
export default client;
Thanks! Could you please include the code for ApolloServer as well? My first take on integrating your code with my ApolloServer code that handles queries/mutations isn’t quite working yet.
Oh okay, maybe there’s a minor but hard-to-find difference in React Native vs. web client in this regard. I couldn’t get it working with web client yet, but I solved my blocker another way. I’m continuing with the excellent swydo:ddp-apollo, which makes the apollo-meteor connections easy to set up.