【问题标题】:How to save dynamic number of variables into database using graphql?如何使用graphql将动态数量的变量保存到数据库中?
【发布时间】:2020-05-15 12:58:51
【问题描述】:

我正在尝试改变和查询动态变量。在将变量发送到服务器之前,用户可以选择添加任意数量的变量。例如,我的应用程序是一个生产力应用程序,它允许用户添加尽可能多的指标来跟踪他们的目标,因此如果“健身房”是他们的目标,那么指标将是“跑步”、“卧推”等。我的问题是,我不确定如何将它们保存在数据库中,因为这些用户创建的变量没有预先配置的架构。

我已经设法使用以下方法将变量发送到后端:

mutation CreateGoal ($title: String!, $description: String, $metric: [Json!]) {
    createGoal(
      data: {
        title: $title
        description: $description
        metric: { set: $metric }
    }
    ){
      id
    }
  } 

架构:

type Mutation { 
    createGoal(data: CreateGoalInput!): Goal!
}

input CreateGoalInput {
    title: String!
    description: String
    metric: GoalCreatemetricInput
}

input GoalCreatemetricInput {
  set: [Json!]
}

一旦变量到达解析器,它就是Json 格式:

{ set: [ 'running', 'bench press' ] }

通常,我只是通过Prisma 保存变量:

 async createGoal(parent, { data }, { request, prisma }, info) {
        const { title, description, metric } = data && data
        return prisma.mutation.createGoal({
            data: {
                user: {
                    connect: {
                        email: user.email
                    }
                },
                title,
                description,
            }
        }, info)
    },

但是,由于变量的数量未知,我如何将“度量”保存到我的数据库中?

如果我要尝试以下方法:

 async createGoal(parent, { data }, { request, prisma }, info) {
        const { title, description, metric } = data && data
        return prisma.mutation.createGoal({
            data: {
                user: {
                    connect: {
                        email: user.email
                    }
                },
                title,
                description,
                metric,
            }
        }, info)
    },

我得到错误:

错误:变量“$_v0_data”的值无效[“running”,“bench press" ] at "_v0_data.metric";字段 "0" 未按类型定义 目标创建度量输入。

如果我要尝试:

 async createGoal(parent, { data }, { request, prisma }, info) {
        const { title, description, metric } = data && data
        return prisma.mutation.createGoal({
            data: {
                user: {
                    connect: {
                        email: user.email
                    }
                },
                title,
                description,
                metric: metric.set
            }
        }, info)
    },

我得到错误:

错误:变量“$_v0_data”的值无效 ["running", "bench press"] at "_v0_data.metric";字段 "0" 未按类型定义 目标创建度量输入。变量“$_v0_data”的值无效 ["Asdfasdf", "Asdfasdf"] 在 "_v0_data.metric";字段“1”不是 由 GoalCreatemetricInput 类型定义。

【问题讨论】:

    标签: node.js reactjs graphql react-apollo prisma


    【解决方案1】:

    我认为您根本不需要使用 Json 标量。看起来您正在尝试传递一个字符串数组,因此您可能只需要使用 [String!] 而不是 [Json!]

    input CreateGoalInput {
        title: String!
        description: String
        metric: [String!]
    }
    

    那你应该可以摆脱

    input GoalCreatemetricInput {
      set: [Json!]
    }
    

    在这里你应该可以将字符串数组传递给后端:

    mutation CreateGoal ($title: String!, $description: String, $metric: [String!]) {
        createGoal(
          data: {
            title: $title
            description: $description
            metric: $metric 
        }
        ){
          id
        }
      } 
    

    在你的解析器中,我认为你需要做的就是:

    async createGoal(parent, { data }, { request, prisma }, info) {
            const { title, description, metric } = data && data
            return prisma.mutation.createGoal({
                data: {
                    user: {
                        connect: {
                            email: user.email
                        }
                    },
                    title,
                    description,
                    metric: { set: metric },
                }
            }, info)
        },
    

    【讨论】:

    • 谢谢。你是对的,我能够毫无错误地保存字符串数组。但是,出于某种原因,我的 Postgres 数据库中不存在 metric 字段。我可以在我的 Graphiql 用户界面中看到它们,但不能在数据库中看到它们。你知道这是为什么吗?
    • 也许检查一下你的 GraphQL 模式和你的 Prisma 模式是否匹配?奇怪的是它出现在 GraphiQL 中而不是 DB 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2014-07-03
    • 2013-05-08
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    相关资源
    最近更新 更多