elasticsearch 如果是真的,那么是假的…如果为假,则为假?

9rnv2umw  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(2)|浏览(82)

我正在构建一个待办事项应用程序,onclick我希望“如果真为假”和“如果假为真”。
我正在使用graphql,在开始使用它之前,一切都是用我现在使用的相同逻辑工作的。现在为什么不呢??

updatedTask: async (parent, args, context, info) => {
      const id = args.id
      const task = await client.get({
        index: 'tasks',
        id: id
      });

      if(task.done){
      await client.update({
          index: 'tasks',
          id: id,
          body: {
            doc: {
              done: false
            }
          }
        });
      }else{
        await client.update({
          index: 'tasks',
          id: id,
          body: {
            doc: {
              done: true
            }
          }
        });
      }
    },

这里是完整的模式

const { makeExecutableSchema } = require('@graphql-tools/schema');
const { resolvers } = require('./resolvers');

const typeDefs = `#graphql

    type Query {
        tasks: [Task!]!
    } 

    type Mutation{
        newTask( input: taskInput!): Task
        updatedTask( id: ID!): Task
        deletedTask( id: ID!): Task
    }

    input taskInput{
        name: String!,
    }

    type Task {
        name: String!
        done: Boolean!
        id: ID!
    }
`
const schema = makeExecutableSchema({ typeDefs, resolvers });
module.exports = {schema}

这里的解析器,我改变了建议,在评论中,我用!task.done,但仍然是同样的问题

const { client } = require('./controllers/tasks');

const resolvers = {
  Query: {
    tasks: async (parent, args, context, info) => {
      const result = await client.search({
        index: 'tasks',
        body: {
          query: {
            match_all: {}
          }
        }
      });
      const tasks = result.hits.hits.map((hit) => hit._source);
      return tasks
    }
  },

  Mutation: {
    
    deletedTask: async (parent, args, context, info) => {
      const id = args.id
      await client.delete({
        index: 'tasks',
        id: id,
      });
    },

    updatedTask: async (parent, args, context, info) => {
      const id = args.id
      const task = await client.get({
        index: 'tasks',
        id: id
      });
      console.log(task.done)
      await client.update({
          index: 'tasks',
          id: id,
          body: {
            doc: {
              done: !task.done
            }
          }
        });
    },

    newTask: async (parent, args, context, info) => {  
      const { input } = args;
      const newTask = {
        name: input.name,
        done: false
      };

      const result = await client.index({             
        index: 'tasks',
        body: newTask
      });

      const { _id } = result;                               
      newTask._id = _id;

      return newTask;
    }
  }
};

module.exports = { resolvers };
ggazkfy8

ggazkfy81#

我不知道graphql,但如果你只是想否定一个值,你可以做!值:

updatedTask: async (parent, args, context, info) => {
      const id = args.id
      const task = await client.get({
        index: 'tasks',
        id: id
      });

      
      await client.update({
          index: 'tasks',
          id: id,
          body: {
            doc: {
              done: ! task.done
            }
          }
        });
}
nwnhqdif

nwnhqdif2#

感谢@大卫,我知道我从来没有一个done属性,我正在寻找的是inside _source.done!
使用!task._source.done解决

相关问题