Skip to content
Advertisement

CORS blocks mutation in GraphQL Yoga

I am working here with a graphql prisma backend and a graphql yoga express server on top of that. In the frontend, I am trying to call a signout mutation but its blocked by the CORS policy. Though I have added cors settings in my graphql yoga server, I keep getting this error. GraphQL Queries are working fine but Mutations are being blocked. My frontend URL is ‘http://localhost:7777‘ and yoga server is running at ‘http://localhost:4444/‘. The Error was:

JavaScript

GraphQL Yoga Server Cors Config:

JavaScript

Mutation:

JavaScript

Please tell me what I am doing wrong here. Thanks in Advance.

Advertisement

Answer

The solution to the problem was to write a middleware that sets appropriate response headers so that the fetch doesn’t fail.

JavaScript

The above is the yoga express server middleware used to solve the problem.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement