【发布时间】:2021-10-15 02:34:30
【问题描述】:
在我的 next.js 应用程序中,我正在尝试配置 Apollo 端点:
import { ApolloServer, gql } from "apollo-server-micro";
// This data will be returned by our test endpoint. Not sure if I need id? https://apuyou.io/blog/serverless-graphql-apollo-server-nextjs
const tacos = {
meat: [
{
type: 'Al Pastor',
imgURL: 'https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/190130-tacos-al-pastor-horizontal-1-1549571422.png?crop=0.668xw:1.00xh;0.175xw,0&resize=480:*'
},
{
type: 'Barbacoa',
imgURL: 'https://i2.wp.com/www.downshiftology.com/wp-content/uploads/2021/02/Barbacoa-Tacos-3.jpg'
},
{
type: 'Chorizo',
imgURL: 'https://www.seriouseats.com/thmb/-8LIIIObcZMUBy-9gXlMsHcaeMI=/610x458/filters:fill(auto,1)/__opt__aboutcom__coeus__resources__content_migration__serious_eats__seriouseats.com__recipes__images__2014__04__20140428-sloppy-joe-chorizo-taco-recipe-food-lab-lite-8-503212a07b0a4d499952ff40aed57694.jpg'
},
],
fish: [
{
type: 'Camaron',
imgURL: 'https://juegoscocinarpasteleria.org/wp-content/uploads/2019/07/1563435179_315_Tacos-De-Camarones-Con-Crema-De-Cal-Y-Cilantro.jpg'
},
{
type: 'Salmon',
imgURL: 'https://www.cookingclassy.com/wp-content/uploads/2015/04/salmon-tacos-with-avocado-salsa4-srgb..jpg'
},
{
type: 'Pulpo',
imgURL: 'https://images.squarespace-cdn.com/content/v1/5710a8b3e707ebb8c58fea2c/1590075315244-QNXQE1LGPH06HV3EDF6B/tacos_34.jpg?format=1000w'
},
],
veggi: [
{
type: 'Cauliflower',
imgURL: 'https://minimalistbaker.com/wp-content/uploads/2017/07/DELICIOUS-Roasted-Cauliflower-Tacos-with-Adobo-Romesco-30-min-healthy-SO-flavorful-vegan-glutenfree-plantbased-cauliflower-tacos-recipe-8.jpg'
},
{
type: 'Avocado',
imgURL: 'https://www.ambitiouskitchen.com/wp-content/uploads/2018/03/tacos.jpg'
},
{
type: 'Tofu',
imgURL: 'http://www.fromachefskitchen.com/wp-content/uploads/2016/08/Tofu-and-Black-Bean-Tacos.jpg'
},
],
}
const typeDefs = gql`
type Taco {
type: String
imgURL: String
}
type Query {
tacos: [Taco]
}
`;
const resolvers = {
Query: {
tacos: () => {
return tacos;
},
},
};
const apolloServer = new ApolloServer({ typeDefs, resolvers });
module.exports = apolloServer.start().then(() => apolloServer.createHandler({path: '/api/graphql',}));
export const config = {
api: {
bodyParser: false,
},
};
但是,http://localhost:3000/api/graphql 让我进入了 Apollo Studio 起始页。当我终于到达资源管理器时,我收到错误消息,无法访问我的服务器:OPTIONS 响应缺少标头access-control-allow-methods: POST。我尝试添加 cors ApolloServer,但没有成功。那是我第一次尝试在 Next.js API 中让 graphql 端点工作,我很迷茫,出了什么问题。
【问题讨论】:
标签: next.js apollo react-apollo apollo-server