DEV Community

KateMLady
KateMLady

Posted on

HTTP and GraphQL

Returning to data transport (and to life, because data needs to be sent between systems), I will say that HTTP perfectly reproduces its functions adjacent to the database.

The classic HTTP method contains:

  1. URL of the place we are accessing
  2. Method Header
  3. Method type (POST, GET, DELETE, PUT)
  4. The body of the request, where the essence of the data is located

There are a couple more HTTP methods, but you don't use the whole table service when you're having dinner, do you? Just a fork, spoon, knife, plate. No more!

function toRequest(
  req: FastifyRequest,
  reply: FastifyReply,
): RawRequest<FastifyRequest, RequestContext> {
  return {
    url: req.url,
    method: req.method,
    headers: req.headers,
    body: req.body as any,
    raw: req,
    context: { reply },
  };
}
Enter fullscreen mode Exit fullscreen mode

In fact, you are familiar with the body method parameter, because it is the JSON I have already described. This is the most convenient structure for using this data section.

export async function parseRequestParams(
  req: FastifyRequest,
  reply: FastifyReply,
): Promise<RequestParams | null> {
  const rawReq = toRequest(req, reply);
  const paramsOrRes = await rawParseRequestParams(rawReq);
  if (!('query' in paramsOrRes)) {
    const [body, init] = paramsOrRes;
    reply
      .status(init.status)
      .headers(init.headers || {})
      .send(body || undefined);
    return null;
  }
  return paramsOrRes;
}
Enter fullscreen mode Exit fullscreen mode

The async function characterizes our type of calls as asynchronous, i.e. no one will stand in line while our server works. Any client will leave a request and return to their business, the answer will come automatically.

These are just the helper parameters and functionality that GraphQL offers to create your own HTTP channel with methods and calls! A more complete description of HTTP can be found on GitHub/created yourself as a training exercise in forwarding calls between systems.

Top comments (0)