Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

GraphQL

Marcel Kloubert edited this page Mar 14, 2023 · 1 revision

Table of contents

About []

GraphQL is a data language for realizing APIs, which are able to query and manipulate data, developed by Facebook / Meta.

Getting started []

Before we can continue, we have to install the following modules:

The following example shows, how simple it is to setup a GraphQL instance, which is quite the same, as doing it with Express:

import createServer, {
  HttpPathValidator,
  HttpRequestHandler,
} from "@egomobile/http-server";
import schema from "./graphql";
import { graphqlHTTP } from "express-graphql";

async function main() {
  const app = createServer();

  // add GraphQL middleware
  // with '/graphql' endpoint
  app.all(
    (request) => request.url!.startsWith("/graphql"),
    [
      graphqlHTTP({
        // [1] https://graphql.org/learn/schema/
        // [2] https://graphql.org/learn/queries/
        schema,
        graphiql: true, // use UI
      }) as any,
    ],
    async () => {
      /** this will never be called, but we need this there */
    }
  );

  // access UI via
  // http://localhost:8080/graphql
  await app.listen(8080);
}

main().catch(console.error);
Clone this wiki locally