I'm currently working on a project that involves a backend using graphql prisma and a frontend with a graphql yoga express server. I've encountered an issue where I am unable to call a signout mutation due to the CORS policy blocking it. Despite adding cors settings to my graphql yoga server, the error persists. While GraphQL Queries work seamlessly, Mutations are restricted. The URL for my frontend is 'http://localhost:7777' and the yoga server is accessible at 'http://localhost:4444/'. Here's the specific error message:
Access to fetch at 'http://localhost:4444/' from origin 'http://localhost:7777' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
[Network error]: TypeError: Failed to fetch
Cors Configuration in GraphQL Yoga Server:
server.start(
{
cors: {
credentials: true,
origin: [process.env.FRONTEND_URL],
},
},
deets => {
console.log(
`Server is now running on port http://localhost:${deets.port}`
);
}
);
Mutation:
// import React, { Component } from 'react';
import { Mutation } from 'react-apollo';
import styled from 'styled-components';
import gql from 'graphql-tag';
import { CURRENT_USER_QUERY } from './User';
import { log } from 'util';
const SIGN_OUT_MUTATION = gql`
mutation SIGN_OUT_MUTATION {
signout {
message
}
}
`;
const SignOutBtn = styled.button`
color: ${props => props.theme.textMedium};
padding: 10px;
margin-right: 20px;
text-align: center;
font-family: garamond-light;
border: 1px solid ${props => props.theme.textMedium};
border-radius: 5px;
transition: background-color 0.5s ease;
transition: color 0.5s ease;
:hover {
background: ${props => props.theme.textMedium};
color: ${props => props.theme.white};
}
`;
const Signout = props => (
<Mutation
mutation={SIGN_OUT_MUTATION}
refetchQueries={[{ query: CURRENT_USER_QUERY }]}
>
{signout => (
<SignOutBtn
onClick={() => {
console.log("comes here")
signout();
}}
>
Sign Out
</SignOutBtn>
)}
</Mutation>
);
export default Signout;
I would appreciate any insights on what could be causing this issue. Thank you!