简体   繁体   English

GraphQL(Prisma)突变数据未定义

[英]GraphQL (Prisma) Mutation data undefined

I'm trying to do a mutation and I keep getting an error about data being null on the React side. 我正在尝试进行突变,并且我一直在React方面得到关于数据为空的错误。 However, if I try the same mutation in the GraphQL console it works. 但是,如果我在GraphQL控制台中尝试相同的变异,它就可以工作。 Also, I know the endpoint is working because I can Query data with no problem. 此外,我知道端点正在工作,因为我可以毫无问题地查询数据。

Everything 一切

Server code (resolver): 服务器代码(解析器):

 async signup(parent, args, ctx, info) {
    // lowercase their email
    args.email = args.email.toLowerCase();
    // hash their password
    const password = await bcrypt.hash(args.password, 10);
    // create the user in the database
    const user = await ctx.db.mutation.createUser({
        data: { 
          ...args,
          password,
        }
      }, info);
    return user;
}

Mutation (React Side) 突变(反应侧)

mutation signupUser($email: String!, $password: String!, $name: String!) {
  signup(email: $email, password: $password, name: $name) {
    __typename
    id
    email
    password
    name
  }
}
TypeError: Cannot read property 'data' of undefined
    at Mutation._this.onMutationCompleted (react-apollo.esm.js:477)
    at react-apollo.esm.js:434

Also here is a snippet of my Mutation on the component 这里还有一个关于组件的Mutation的片段

<Mutation
        mutation={signUpUserMutation}
        onCompleted={(user) => {
          handleClose();
        }}
        onError={(error) => {
          console.log(error)
          setOpen(true);
        }}
      >
        {signup => (
          <Form
            onSubmit={async (values, { setSubmitting }) => {
              await signup({
                variables: {
                  name: values.name,
                  email: values.email,
                  password: values.password,
                },
              });
              setSubmitting(false);
            }}
          >
            {({
              values, errors, handleChange, handleBlur, isSubmitting,
            }) => (

In your schema 在您的架构中

type User {
    id: Int
    name: String
    email: String
    password: String
}
type Response {
    status: Boolean
    message: String
    data: User
}
type Mutation {
    signUp(name: String!, email: String!, password: String!) : Response
}   

On Mutation resolver signUp function On Mutation resolver signUp函数

 async signUp(parent, args, { db }, info) {
    // lowercase their email
    args.email = args.email.toLowerCase();
    // hash their password
    const password = await bcrypt.hash(args.password, 10);
    // create the user in the database
    const data = { 
        ...args, 
        password,
    }
    const user = await db.mutation.createUser(data, info);
    return user;
 }

in database mutation createUser function you can access like that 在数据库变异createUser函数中,您可以像这样访问

const createUser = async ({ name, email, password }, info) => {
  try {
      // Code to save user information in database
    return { status: true,
             message: "User registration successful. You can login now.", 
             data: createdUser
    }
  } catch(e) {
    return { status: false, message: e.message, data: null }
  }
}

And your query 和你的查询

mutation signupUser($email: String!, $password: String!, $name: String!) {
  signup(email: $email, password: $password, name: $name) {
    status
    message
    data {
     id
     email
     password
     name
    }
  }
}

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

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