代码之家  ›  专栏  ›  技术社区  ›  MitchEff

我是应该一遍又一遍地重新输入我的字段,还是我认为这是错误的?

  •  3
  • MitchEff  · 技术社区  · 7 年前

    我有点像在阿波罗/格拉霍克周围转来转去。/ Prisma /瑜伽,但有一点我总是坚持,就是,有这么多的加倍进行。

    假设我有一个名为 Client ,它有 title , firstName , lastName , email , phone , address

    当我进行突变时,我需要输入所有字段:

    const result = await this.props.saveClientMutation({
      variables: {
        title,
        firstName,
        lastName,
        email,
        (etc)
      }
    })
    

    然后转到我的客户机中的实际graphql定义,在那里我再次键入所有字段(两次!)

    mutation SAVE_CLIENT_MUTATION ($title: String!, $firstName: String!, $lastName: String!, $email: String!) {
      login(title: $title, firstName: $firstName, lastName: $lastName, email: $email) {
        client {
          id
          firstName
          lastName
        }
      }
    }
    

    然后转到服务器中的解析器(感谢spread操作符),然后转到数据库模式,在这里我第四次基本上键入所有相同的字段。

    这看起来像是一个庞大的表面区域,存在漏洞和不一致。我是否彻底误解了这意味着如何工作,或者是否意味着要进行如此疯狂的重新输入?

    1 回复  |  直到 7 年前
        1
  •  2
  •   ed'    7 年前

    你可以试着利用…

    …但据我所知,您仍需要重新键入许多字段

    一个接口至少应该在一个类型不能正确实现它时抛出一个错误,并且输入类型和片段应该为您保存一些重新输入(如果它们可以重用的话)。

    # Reusable type fields
    interface IClient {
        title: String
        firstName: String
        lastName: String
        email: String
        phone: String
        address: String
    }
    
    type Client implements IClient {
        # You must re-type interface items
        title: String
        firstName: String
        lastName: String
        email: String
        phone: String
        address: String
    }
    
    # Reusable mutation input variables
    input ClientInput {
        title: String
        firstName: String
        lastName: String
        email: String
        phone: String
        address: String
    }
    
    # Reusable query fields
    fragment ClientParts on Client {
      firstName
      lastName
    }
    
    # You can use your input type & fragment here
    # although the input does change the structure from your example
    mutation SAVE_CLIENT_MUTATION ($input: ClientInput!) {
        login(input: $input) {
          client {
            ...ClientParts
            id
          }
        }
    }
    
    推荐文章