[Graphql + Netlify] Solve @Apollo/client CORS problem

In functions/graphql/index.js file:

const { ApolloServer, gql } = require("apollo-server-lambda");

const typeDefs = gql`
  type Query {
    todos: [Todo]!
  }
  type Todo {
    id: ID!
    text: String!
    done: Boolean!
  }
  type Mutation {
    addTodo(text: String!): Todo
    updateTodoDone(id: ID!): Todo
  }
`;

const todos = {};
let todoIndex = 0;
const resolvers = {
  Query: {
    todos: () => Object.values(todos),
  },
  Mutation: {
    addTodo: (_, { text }) => {
      todoIndex++;
      const id = `key-${todoIndex}`;
      todos[id] = { id, text, done: false };
      return todos[id];
    },
    updateTodoDone: (_, { id }) => {
      todos[id].done = true;
      return todos[id];
    },
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  // should be disabled in productions
  playground: true,
  introspection: true,
});
//Add CORS
exports.handler = server.createHandler({
  cors: {
    origin: "*",
    credentials: true,
  },
});
上一篇:Laravel 通过 Passport 实现 API 请求认证:第三方应用篇(授权码获取令牌)


下一篇:passport策略之passport-facebook使用facebook认证