gql icon indicating copy to clipboard operation
gql copied to clipboard

405 and CORS

Open avalero opened this issue 3 years ago • 2 comments
trafficstars

Hello, I have a server configured with http as follows:

const handler = async (req: Request) => {
    const { pathname } = new URL(req.url);

    return pathname === "/graphql"
      ? await GraphQLHTTP<Request, Context>({
          schema: makeExecutableSchema({
            resolvers,
            typeDefs: [center, student, instructor, group, scalars],
          }),
          graphiql: true,
          context: () => {
            return { request: req };
          },
        })(req)
      : new Response("Not Found", { status: 404 });
  };

  const server = new Server({ handler });
  const listener = Deno.listen({ port: parseInt(PORT) });

  console.info("Listening on", listener.addr);

  await server.serve(listener);

When I use the graphiql playground it works just fine, but when using from any other client I get 2 erros:

  • Request Method: OPTIONS Status Code: 405 Method Not Allowed
  • CORS

How could I fix this? Thanks for your time.

avalero avatar Aug 08 '22 14:08 avalero

regarding CORS I don't know why the error happens because it's not controlled by gql, but for OPTIONS it's a bug.

If you want, you can create a PR for it, or I'll fix it later myself, seems very simple to fix.

talentlessguy avatar Aug 09 '22 12:08 talentlessguy

@avalero The solution is not from the library but from how you are handling CORS with your app.

Please see an example with CORS here: https://github.com/MichaelFedora/gql/blob/vanilla-cors/examples/vanilla.ts

--

I say this because OPTIONS is correctly handled by oak (and probably other http libraries), so if you are using vanilla, you are going to have to do it manually regardless.

MichaelFedora avatar Oct 07 '22 17:10 MichaelFedora