【发布时间】:2019-05-16 10:52:14
【问题描述】:
我在这里使用的是 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;
请告诉我我在这里做错了什么。提前致谢。
【问题讨论】:
-
我看不出您共享的代码有什么问题。您能否提供一个最低限度的可重现存储库供我们进行更多调查?
-
@Errorname 感谢您的帮助,您可以找到 repo here。回购将没有环境文件。如果您需要这些,请告诉我。
-
感谢您提供链接,但要分析的文件太多。您能否创建一个仅包含重现问题所需文件的存储库?
标签: javascript express graphql react-apollo prisma-graphql