简体   繁体   English

GraphQL突变如何将自身与类型关联

[英]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? GraphQL如何知道createUser与我的User模型相关联? I could set it up so that createUser returns token instead of User (after generating a token) or I could rename createUser to createPerson . 我可以进行设置,以便createUser返回令牌而不是User(生成令牌后),也可以将createUser重命名为createPerson I have never defined the association between createUser and User . 我从未定义过createUserUser之间的关联。 I'm unsure how my data input through createUser gets directed to be saved in the user table, instead of another table. 我不确定通过createUser输入的数据如何定向保存在user表中,而不是在另一个表中。

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. prisma.mutation.createUser返回一个Promise,该Promise解析为一个代表所创建用户的对象。 This object happens to have properties that match the User type you specified in your schema, but otherwise there's nothing "special" about it. 该对象碰巧具有与您在架构中指定的User类型匹配的属性,但是除此之外,没有任何“特殊”的东西。

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

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