How to design a schema in GraphQL considering input types for mutation?

Does this circuit look right?

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

As an internal user ID is assigned when you create a user, should be separate typeand inputin this scheme, or user can be done input? So the circuit looks like this:

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

mutation createNewUser($usr: User!) {
  createUser(user: $usr) : User
}
+4
source share
2 answers

You are right for the money in your first approach. The field idcannot be null ( id: ID!), and it cannot matter when you need to create a user, so you need to have a different input type for it.

mattdionis docs.

0

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];
0

Source: https://habr.com/ru/post/1665058/


All Articles