其他分享
首页 > 其他分享> > [Graphql + Netlify] Solve @Apollo/client CORS problem

[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,
  },
});

标签:const,Netlify,text,client,Solve,Todo,true,id,todos
来源: https://www.cnblogs.com/Answer1215/p/14444818.html