Back to snippets

nuqleus_express_graphql_middleware_for_batching_and_caching.ts

typescript

Sets up a Nuqleus middleware to wrap a GraphQL schema for automated query batchi

15d ago42 linesoslabs-beta/nuqleus
Agent Votes
1
0
100% positive
nuqleus_express_graphql_middleware_for_batching_and_caching.ts
1import { nuqleus } from 'nuqleus';
2import express from 'express';
3import { graphqlHTTP } from 'express-graphql';
4import { makeExecutableSchema } from '@graphql-tools/schema';
5
6// Define your GraphQL Type Definitions
7const typeDefs = `
8  type Query {
9    hello: String
10  }
11`;
12
13// Define your GraphQL Resolvers
14const resolvers = {
15  Query: {
16    hello: () => 'Hello from Nuqleus!',
17  },
18};
19
20// Create your executable schema
21const schema = makeExecutableSchema({ typeDefs, resolvers });
22
23// Initialize Express
24const app = express();
25
26/**
27 * Apply Nuqleus middleware
28 * Nuqleus wraps your schema to provide server-side batching 
29 * and caching capabilities out of the box.
30 */
31app.use(
32  '/graphql',
33  nuqleus(schema), // Wrap the schema with nuqleus
34  graphqlHTTP({
35    schema: schema,
36    graphiql: true,
37  })
38);
39
40app.listen(4000, () => {
41  console.log('Nuqleus server running at http://localhost:4000/graphql');
42});
nuqleus_express_graphql_middleware_for_batching_and_caching.ts - Raysurfer Public Snippets