zoukankan      html  css  js  c++  java
  • [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,
      },
    });
    
  • 相关阅读:
    易语言常用源码
    ci的数据库地址
    格式化输出php数组
    mysql插入表情问题
    线程、进程与协程2
    线程、进程与协程
    if __name=='__main__"的作用
    动态导入模块
    面向对象补充之方法
    getpass模块
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14444818.html
Copyright © 2011-2022 走看看