Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace express-graphql with GraphQL Helix middleware #311

Merged
merged 1 commit into from
Jan 25, 2022

Conversation

koistya
Copy link
Member

@koistya koistya commented Jan 25, 2022

  • Replace express-graphql (poorly maintained) with GraphQL Helix middleware
  • Update GraphQL middleware in api/graphql.ts
  • Move GraphQL schema object from api/graphql.ts to api/schema.ts
  • Update project dependencies to the latest versions
  • Convert yarn setup script from CommonJS to ESM
import express from "express";
import {
  getGraphQLParameters,
  processRequest,
  renderGraphiQL,
  sendResult,
  shouldRenderGraphiQL,
} from "graphql-helix";
import { Context } from "./context";

// GraphQL API middleware
app.use("/api", express.json(), async function api(req, res, next) {
  try {
    if (shouldRenderGraphiQL(req)) {
      res.send(renderGraphiQL({ endpoint: "/api" }));
    } else {
      const params = getGraphQLParameters(req);
      const result = await processRequest<Context>({
        operationName: params.operationName,
        query: params.query,
        variables: params.variables,
        request: req,
        schema,
        contextFactory: () => new Context(req),
      });

      sendResult(result, res, (result) => ({
        data: result.data,
        errors: result.errors?.map((err) => {
          // Report errors except for user input validation errors
          if (!(err.originalError instanceof ValidationError)) {
            reportError(err, req, params);
          }
          return err;
        }),
      }));
    }
  } catch (err) {
    next(err);
  }
}

Ref graphql/express-graphql#797

@koistya koistya merged commit 82a7175 into main Jan 25, 2022
@koistya koistya deleted the graphql-middleware branch January 25, 2022 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant