0

I'm trying to integrate GraphQL subscriptions in my SvelteKit application using Apollo Server and the Neo4j GraphQL Library. I have successfully set up the basic GraphQL queries and mutations, but I'm struggling with implementing subscriptions.

Here's a simplified version of my code of my src/routes/graphql/+server.ts file:

import neo4j from 'neo4j-driver';
import { Neo4jGraphQL } from '@neo4j/graphql';
import { ApolloServer, gql } from 'apollo-server-svelte-kit';

const driver = neo4j.driver('bolt://localhost:7687', neo4j.auth.basic('neo4j', 'password'));

const typeDefs = gql`
  type User {
    id: ID! @id
    username: String!
    password: String! @private
    posts: [Post!]! @relationship(type: "POSTED_BY", direction: OUT)
  }

  type Post {
    id: ID! @id
    title: String!
    content: String!
    author: User! @relationship(type: "POSTED_BY", direction: IN)
  }

  type Query {
    users: [User]
    posts: [Post]
  }

  type Mutation {
    signIn(username: String!, password: String!): User
    signUp(username: String!, password: String!): User
  }
`;

const resolvers = {
  // resolver logic
};

const neoSchema = new Neo4jGraphQL({
  typeDefs: typeDefs,
  driver: driver,
  resolvers: resolvers
});

const server = new ApolloServer({
  schema: await neoSchema.getSchema(),
  context: ({ req }) => {
    return {
      driver: driver,
      req: req
    };
  }
});

await server.start();

export const POST = server.handleRequest;

I would like to know how to implement GraphQL subscriptions using Apollo Server and the Neo4j GraphQL Library in SvelteKit. Specifically, I want to be able to subscribe to real-time updates for certain queries, such as receiving new posts as they are created.


First, I attempted to import the WebSocketServer class from the "ws" package and the Neo4jGraphQLSubscriptionsSingleInstancePlugin from @neo4j/graphql. I added the Neo4jGraphQLSubscriptionsSingleInstancePlugin to the list of plugins in the Neo4jGraphQL constructor.

The example code provided in the Neo4j GraphQL Library documentation, which uses Express and createServer from the "http" package to create an HTTP server. Then, it initializes a WebSocketServer instance by passing the HTTP server object to it. This approach didn't work in SvelteKit since the HTTP server is internally handled by SvelteKit, and I couldn't properly initiate the WebSocketServer.

Here's an example code snippet from the documentation that uses Express:

// ... previous code ...

async function main() {
    // Apollo server setup with WebSockets
    const app = express();
    const httpServer = createServer(app);
    const wsServer = new WebSocketServer({
        server: httpServer,
        path: "/graphql",
    });

    // Neo4j schema
    const schema = await neoSchema.getSchema();

    const serverCleanup = useServer(
        {
            schema,
            context: (ctx) => {
                return ctx;
            },
        },
        wsServer
    );

    const server = new ApolloServer({
        schema,
        plugins: [
            ApolloServerPluginDrainHttpServer({
                httpServer
            }),
            {
                async serverWillStart() {
                    return Promise.resolve({
                        async drainServer() {
                            await serverCleanup.dispose();
                        },
                    });
                },
            },
        ],
    });
}

main();

In SvelteKit, the underlying server setup and management are abstracted away, and we don't have direct access to the HTTP server object.

I was expecting to find a way to integrate the WebSocket functionality required for subscriptions within the SvelteKit framework, or an alternative approach to enable GraphQL subscriptions in my SvelteKit application using Apollo Server and the Neo4j GraphQL Library.

If anyone has successfully implemented GraphQL subscriptions in SvelteKit using Apollo Server and the Neo4j GraphQL Library or has any insights or code examples, I would greatly appreciate your guidance. Thank you!

gwern
  • 1

0 Answers0