不确定什么地方出了错或我在设置中遗漏了什么,但是我无法执行查询。出现以下错误
Access to fetch at 'http://localhost:4000/' from origin 'http://localhost:3000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
这是我的Apollo Client设置
import React from 'react';
import ApolloClient, { InMemoryCache } from "apollo-boost";
import { ApolloProvider } from "react-apollo";
import { ApolloProvider as ApolloHooksProvider } from '@apollo/react-hooks';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
const client = new ApolloClient({
uri: "http://localhost:4000/",
cache: new InMemoryCache(),
request: operation => {
operation.setContext({
fetchOptions: {
credentials: 'include',
},
});
},
});
ReactDOM.render(
<ApolloProvider client={client}>
<ApolloHooksProvider client={client}>
<App />
</ApolloHooksProvider>
</ApolloProvider>,
document.getElementById('root')
);
serviceWorker.unregister();
这是我服务器上的设置
const { prisma } = require('../generated/prisma-client');
const { GraphQLServer } = require('graphql-yoga');
const Query = require('./resolvers/Query');
const Mutation = require('./resolvers/Mutation');
const cookieParser = require('cookie-parser');
const jwt = require('jsonwebtoken');
const cors = require('cors');
require('dotenv').config({ path: '.env' });
const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers: {
Query,
Mutation
},
resolverValidationOptions: {
requireResolversForResolveType: false,
},
context: req => ({
prisma,
...req,
}),
});
const corsOptions = {
origin: 'http://localhost:3000',
credentials: true
};
server.express.use(cookieParser());
server.express.use(cors(corsOptions));
server.express.use((req, res, next) => {
const { token } = req.cookies;
if(token) {
const { userId } = jwt.verify(token, process.env.APP_SECRET);
req.userId = userId;
}
next();
})
server.start(details => {
console.log(`Server is now running on http://localhost:${details.port}`);
})