【问题标题】:How to design schema in GraphQL considering input types for mutation?考虑到突变的输入类型,如何在 GraphQL 中设计模式?
【发布时间】:2017-05-12 07:21:49
【问题描述】:

这个架构看起来正确吗?

type User {
    id : ID!
    username : String!
    email : String!
    name : String!
}

input UserInput {
    username : String!
    email : String!
    name : String!
}

mutation createNewUser($usr: UserInput!) {
  createUser(user: $usr)
}

由于用户的内部 id 将在创建用户时分配,因此该架构中是否应该有单独的 typeinput 或者 User 可以设为 input?所以架构看起来像这样

input User {
    id: ID
    username : String!
    email : String!
    name : String!
}

mutation createNewUser($usr: User!) {
  createUser(user: $usr) : User
}

【问题讨论】:

标签: graphql graphql-js


【解决方案1】:

您的第一种方法是正确的。 id字段不能为空(id: ID!),并且在需要创建用户时不能有值,所以需要另一种输入类型。

正如mattdionis 也指出的那样,docs 中有一个非常相似的样本。

【讨论】:

    【解决方案2】:

    你的架构应该是

    const typeDefinitions = `
    type User {
        id : ID!
        username : String!
        email : String!
        name : String!
    }
    
    input UserInput {
        username : String!
        email : String!
        name : String!
    }
    
    type Mutation {
      createUser(user: UserInput) User
    }
    
    schema {
      mutation:Mutation
    }
    `;
    
    export default [typeDefinitions];
    

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 2020-11-19
      • 2020-07-21
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 2021-08-20
      • 2021-04-19
      • 1970-01-01
      相关资源
      最近更新 更多