我在这里使用 graphql prisma 后端和 graphql yoga express 服务器。在前端,我试图调用一个注销突变,但它被 CORS 策略阻止。尽管我在我的 graphql 瑜伽服务器中添加了 cors 设置,但我一直收到此错误。GraphQL 查询工作正常,但突变被阻止。我的前端 URL 是“ http://localhost:7777 ”,瑜伽服务器在“ http://localhost:4444/ ”运行。错误是:
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
GraphQL Yoga Server Cors 配置:
server.start(
{
cors: {
credentials: true,
origin: [process.env.FRONTEND_URL],
},
},
deets => {
console.log(
`Server is now running on port http://localhost:${deets.port}`
);
}
);
突变:
// 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;
请告诉我我在这里做错了什么。提前致谢。