encoredev/encore

Feature Request: GraphQL support

oo00spy00oo opened this issue · 1 comments

How to implement GraphQL support in Encore?

Take a look at: https://github.com/dotansimha/graphql-yoga

How to implement GraphQL support in Encore?

Take a look at: https://github.com/dotansimha/graphql-yoga

Implement a raw API call and massage the request type from encore to fit yogas requirements and response to fit whatever encore wants.

Maybe it's raw node req/responses or Express or https://developer.mozilla.org/en-US/docs/Web/API/Request

mport { createYoga } from 'graphql-yoga'
 
const yoga = createYoga<SomeContext>()
 
export async function serveGraphQLRequest(req: WhatEverRequestEncoreGives, ctx: SomeContext) -> WhateverResponseEncoreExpects {
  const response = await yoga.fetch(
    req.url,
    {
      method: req.method,
      headers: req.headers,
      body: req.body 
    },
    ctx
  )
 
  const headersObj = Object.fromEntries(response.headers.entries())
 
  // Let's say your environment needs to return something like the below;
const encoreResponse: WhateverResponseEncoreExpects =  {
    statusCode: response.status,
    body: response.body,
    headers: headersObj 
  }
return encoreResponse

}

export const graphQLApiPoint = api.raw(
  { expose: true, path: "/api/gql", method: "POST" },
  async (req, resp) => {
  return  serveGraphQLRequest(req) // Let the type checker help you. 
  },
);