Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungWhy Claude Code keeps writing shell commands that fail on your Mac(20.09.2026 um 21:06 Uhr)
Sichere Programmierungllms.txt v2: What the Spec Says, and What 137,000 Domains Show(20.09.2026 um 21:17 Uhr)
Sicherheitslücken (CVE)NiceTryGPT: Less pattern matching. More actual hacking.(20.09.2026 um 21:19 Uhr)
IT Security VideoActivities BoF (kde2026)(20.09.2026 um 00:00 Uhr)
IT Security Toolsirdoc-app(20.09.2026 um 20:33 Uhr)
Sichere ProgrammierungWhy Claude Code keeps writing shell commands that fail on your Mac(20.09.2026 um 21:06 Uhr)
Sichere Programmierungllms.txt v2: What the Spec Says, and What 137,000 Domains Show(20.09.2026 um 21:17 Uhr)
Sicherheitslücken (CVE)NiceTryGPT: Less pattern matching. More actual hacking.(20.09.2026 um 21:19 Uhr)
IT Security VideoActivities BoF (kde2026)(20.09.2026 um 00:00 Uhr)
IT Security Toolsirdoc-app(20.09.2026 um 20:33 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Setup GraphQL Mock Server

Reagiere als Erste:r — dein Feedback zählt!

Introduction

This tutorial will guide you through creating a GraphQL mock server for your JavaScript application. A mock server is invaluable for local development, enabling seamless testing when the backend is unavailable or while running E2E tests.

Prerequisites

Before proceeding, ensure the following tools are installed on your system:

  • Node.js
  • npm/yarn/pnpm

Step-by-step Guide

1. Install Dependencies

Begin by installing the necessary dependencies with npm or yarn:

npm install --save-dev graphql graphql-tag graphql-tools @apollo/server node-fetch

2. Setup Project Structure

Set up your project directory as follows:

/graphql-mock-server
  |-- localSchema.ts
  |-- schema.graphql
  |-- server.ts

3. Local Schema

Define your local schema in localSchema.ts. This schema will override remote schema definitions for specific queries or mutations. Provide resolvers to return predefined mock data.

// filepath: /graphql-mock-server/localSchema.ts
import { makeExecutableSchema } from '@graphql-tool/schema'
import { gql } from 'graphql-tag'

const typeDefs = gql`
  type Query {
    myQuery: String
  }
`;

const resolvers = {
  Query: {
    myQuery: () => 'This is mock data'
  },
};

export const localSchema = makeExecutableSchema({typeDefs, resolvers});

4. Remote Schema

The remote schema is loaded from the local file schema.graphql. This file should include the schema definition, which can be exported from your GraphQL playground.

// filepath: /graphql-mock-server/schema.graphql
type Query {
  myQuery: String
}

5. Mock Server Implementation

The mock server stitches the local and remote schemas, giving precedence to the local schema-specified queries or mutations.

// filepath: /graphql-mock-server/server.ts
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { stitchSchema } from '@graphql-tools/stitch';
import { loadSchemaSync } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { localSchema } from './localSchema';
import { wrapSchema } from '@graphql-tools/wrap';
import { print } from 'graphql';
import fetch from 'node-fetch';

async function startServer(){
  const remoteSchema = loadSchemaSync('graphql-mock-server/schema.graphql',{
    loaders: [new GraphQLFileLoader()],  
  });

  const executableRemoteSchema = wrapSchema({
    schema: remoteSchema,
    executor: async ({ document, variables }) => {
      const query = print(document);
      const fetchResult = await fetch(
          'https://graphql.server.com/graphql',
          {
          method: 'POST',
          headers: { 'Content-Type': 'application/json'},
          body: JSON.stringify({ query, variables}),
          },
        );
       return fetchResult.json();
     },
   });

  const schema = stitchSchema ({
    subschema: [
        { schema: executableRemoteSchema },
        { schema: localSchema },
        merge: {
          myQuery: { //override the Query/Mutation here
            selectionSet: '{ __typename }',
            resolve: (parent, args, context, info) => {
               return localSchema
                   .getQueryType()
                   ?.getFields()
                   .myQuery.resolve(parent, args, context, info);
               },
             },
           },
          },
        },
       ],
     });

   const server = ApolloServer({ schema });
   const {url } = await startStandaloneServer( server, {
       listen: { port: 4000},
   });

  console.log(`🚀 Server ready at ${url}`);
}

startServer();

If you have reached here, then I made a satisfactory effort to keep you reading. Please be kind enough to leave any comments or share corrections.

My Other Blogs:

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Setup GraphQL Mock Server

Thematisch verwandte Begriffe: Setup, GraphQL, Mock, Server · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93956 | A flaw has been found in olivier-ls PHP-FTS up to 1.1.2. Affected by thi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick