简体   繁体   English

GraphQL嵌套突变+序列化

[英]GraphQL nested mutation + Sequelize

I am using graphQL, sequelize and nodejs with apollo client. 我正在使用带有Apollo客户端的graphQL,sequelize和nodejs。 The data which I am sending from the UI is a nested structure of the following form: 我从UI发送的数据是以下形式的嵌套结构:

Grandparent 祖父母

Parent 父级

Child 儿童

So a grandparent can have one or more parents and a parent can have one or more children. 因此,祖父母可以有一个或多个父母,而父母可以有一个或多个孩子。

I am trying to update my SQLite database, created using sequelize, using one GraphQL mutation. 我正在尝试使用一个GraphQL突变来更新使用后继创建的SQLite数据库。 I am able to create a mutation with a flat structure: 我能够创建具有平坦结构的突变:

schema.gql file contents -> schema.gql文件内容->

type Mutation Family(
    grandParentName: String,
    grandParentHeight: String,
    Parent: String,
)

but I want something along the lines of 但我想要一些类似的东西

type Mutation CreateNestedFamily(
    grandParentName: String,
    grandParentHeight: String,
    Parent: CreateParent(
           parentName: String,
           parentHeight: String
           child: CreateChild(
                 childName: String,
                 childHeight: String
                 )
            )
)

But I dont know how to achieve this and the graphql documentation around nested mutations is very limited. 但是我不知道该如何实现,有关嵌套突变的graphql文档非常有限。

Any help would be appreciated! 任何帮助,将不胜感激!

The way you've phrased you problem is a little strange, but i'll do my best to point you in the right direction. 您说出问题的方式有些奇怪,但是我会尽力为您指明正确的方向。

There's no such thing as a nested mutation in GraphQL, but you can construct input types that allows an arbitrary nesting of data that you pass to a single mutation. 在GraphQL中没有嵌套变异之类的东西,但是您可以构造输入类型,该输入类型允许传递给单个变异的数据的任意嵌套。

I'll step back from your approach and take a specific problem "I want to create a person and their descendants" 我将退后一步,提出一个具体的问题:“我想创建一个人及其后代”

input PersonInput {
  name: String!
  height: String!
  children: [PersonInput!]
}

type Mutation {
  createPerson(person: PersonInput!): SomeOutputType
}

Hopefully you can see the recursive nature of this structure. 希望您可以看到此结构的递归性质。 It'd be more difficult to impose a limit on this, ie to only allow 3 levels deep. 对此施加限制比较困难,即仅允许3个深度。

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

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