PhelipeLB / Crud-beer

App project to help taking order for a local beer store.
11 stars 1 forks source link

implement a graphql server #11

Closed OtavioSC closed 1 year ago

OtavioSC commented 1 year ago

I guess we could use GraphQL Helix to implement the graphql server, because this tool combine a lot of useful features in a single one framework.

Example:

     // server.ts

      import {
          getGraphQLParameters, 
          processRequest, 
          renderGraphiQL, 
          shouldRenderGraphiQL, 
          sendResult
      } from "graphql-helix";

      import { schema } from "./graphql/schema";

      import Koa from "koa";
      import cors from '@koa/cors'
      import bodyParser from 'koa-bodyparser'

      const app = new Koa();

      app.use(bodyParser())
      app.use(cors())

      export default app.use(async (ctx) => {

          const request = {
              body: ctx.request.body,
              headers: ctx.request.headers,
              method: ctx.request.method,
              query: ctx.request.query,
            };

          if (shouldRenderGraphiQL(request)) {
              ctx.body = renderGraphiQL({});
            } else {
              const { operationName, query, variables } = getGraphQLParameters(request);

              const result = await processRequest({
                operationName,
                query,
                variables,
                request,
                schema,
              });

              sendResult(result, ctx.res);
          }
      })
NandoGoulartt commented 1 year ago

i agree about GraphQL

daniloab commented 1 year ago

send a pr