简体   繁体   中英

How does GraphQL Mutation associate itself with Type

When I set up my schema as following:

type Mutation {
    createUser(data: CreateUserInput!): User!
}

type User {
    id: ID!
    name: String!
    password: String!
    email: String!
    posts: [Post!]!
    comments: [Comment!]!
}

and my resolver:

const Mutation = {
    async createUser(parent, args, { prisma }, info) {
        if(args.data.password.length < 8) {
            throw new Error("Pasword must be 8 characters or longer")
        }
        return prisma.mutation.createUser({ 
            data: {
                ...args.data,
                password
            } 
        })
    }
}

how does GraphQL know that createUser is associated with my User model? I could set it up so that createUser returns token instead of User (after generating a token) or I could rename createUser to createPerson . I have never defined the association between createUser and User . I'm unsure how my data input through createUser gets directed to be saved in the user table, instead of another table.

There is no association between the two.

Your resolver could just as easily return a plain object with some dummy data:

async createUser(parent, args, { prisma }, info) {
  ...
  return { 
    id: 1,
    name: 'Kevvv',
    password: 'password',
    email: 'kevvv@stackoverflow.com',
  }
}

or use some other means of fetching the user:

async createUser(parent, args, { prisma }, info) {
  ...
  return fetchUserFromAPI()
  // or some other data source like a DB
}

prisma.mutation.createUser returns a Promise that resolves to an object which represents the created user. This object happens to have properties that match the User type you specified in your schema, but otherwise there's nothing "special" about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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