简体   繁体   English

如何使用graphql将动态数量的变量保存到数据库中?

[英]How to save dynamic number of variables into database using graphql?

I'm trying to mutate and query dynamic variables.我正在尝试改变和查询动态变量。 The user has the choice to add as many variables as they want before sending them off to the server.在将变量发送到服务器之前,用户可以选择添加任意数量的变量。 For example, my app is a productivity app that allows a user to add as many metrics as they want to track their goal so if "Gym" is their goal, the metrics would be "running", "bench press", etc. My problem is, I'm unsure how to save them in the database since there is no pre-configured Schema for these user-created variables.例如,我的应用程序是一个生产力应用程序,它允许用户添加尽可能多的指标来跟踪他们的目标,所以如果“健身房”是他们的目标,那么指标将是“跑步”、“卧推”等。我的问题是,我不确定如何将它们保存在数据库中,因为这些用户创建的变量没有预配置的架构。

I've managed to send the variables to the back end using the following:我设法使用以下方法将变量发送到后端:

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

Schema:架构:

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

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

input GoalCreatemetricInput {
  set: [Json!]
}

Once the variables arrive in the resolver, it's in the Json format:一旦变量到达解析器,它就是Json格式:

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

Normally, I'd simply save the variables through Prisma :通常,我只是通过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)
    },

However, since the number of variables are unknown, how do I save 'metric' into my database?但是,由于变量的数量未知,如何将“度量”保存到我的数据库中?

If I were to try the following:如果我要尝试以下操作:

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

I get the error:我收到错误:

Error: Variable "$_v0_data" got invalid value [ "running", "bench press" ] at "_v0_data.metric";错误:变量“$_v0_data”在“_v0_data.metric”处得到无效值[“running”,“bench press”]; Field "0" is not defined by type GoalCreatemetricInput.字段“0”不是由 GoalCreatemetricInput 类型定义的。

If I were to try:如果我要尝试:

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

I get the error:我收到错误:

Error: Variable "$_v0_data" got invalid value ["running", "bench press"] at "_v0_data.metric";错误:变量“$_v0_data”在“_v0_data.metric”处得到无效值[“running”,“bench press”]; Field "0" is not defined by type GoalCreatemetricInput.字段“0”不是由 GoalCreatemetricInput 类型定义的。 Variable "$_v0_data" got invalid value ["Asdfasdf", "Asdfasdf"] at "_v0_data.metric";变量 "$_v0_data" 在 "_v0_data.metric" 处得到无效值 ["Asdfasdf", "Asdfasdf"]; Field "1" is not defined by type GoalCreatemetricInput.字段“1”不是由 GoalCreatemetricInput 类型定义的。

I don't think you need to use the Json scalar at all.我认为您根本不需要使用Json标量。 It looks like you're trying to pass an array of strings so instead of [Json!] you may just need to use [String!] .看起来您正在尝试传递一个字符串数组,因此您可能只需要使用[String!]而不是[Json!] [String!]

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

Then you should be able to get rid of那么你应该能够摆脱

input GoalCreatemetricInput {
  set: [Json!]
}

Here you should be able to pass the array of strings to the backend:在这里,您应该能够将字符串数组传递给后端:

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

And in your resolover I think all you need to do is:在您的解析器中,我认为您需要做的就是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM