I created a resolver and graphql file about user creation.
The data type seems to fit, and I have asynchronous processing, but the result always return null. I don't know why.
- Resolver (createAccount.js)
import { prisma, generateToken, changePhoneNumber } from "../../../utils";
import bcrypt from "bcryptjs";
export default {
Mutation: {
createAccount: async (_, args) => {
const {
username,
password,
password2,
email,
phoneNum,
bio,
avatarUrl,
} = args;
if (password !== password2) {
throw new Error("two password aren't same each other");
}
const encryptPw = await bcrypt.hash(password, 10);
const newPhoneNumber = await changePhoneNumber(phoneNum, "+82");
const user = await prisma.user.create({
data: {
username,
email,
password: encryptPw,
phoneNum: newPhoneNumber,
bio,
avatarUrl,
},
});
const token = generateToken(user.id);
return { token, user };
},
},
};
- GraphQL file (createAccount.graphql)
type Mutation {
createAccount(
username: String!
email: String!
password: String!
password2: String!
phoneNum: String!
bio: String
avatarUrl: String
): AuthPayload!
}
type AuthPayload {
token: String
user: User
}
- utils.js
import { PrismaClient } from "@prisma/client";
import jwt from "jsonwebtoken";
export const prisma = new PrismaClient();
export const changePhoneNumber = (phoneNum, locationNum) => {
var leftStr = locationNum;
var rightStr = phoneNum.slice(1, phoneNum.length);
var newStr = leftStr + rightStr;
return newStr;
};
export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);
- models.graphql
type User {
id: ID!
avatarUrl: String
username: String!
email: String!
password: String!
phoneNum: String!
emailSecret: String
phoneSecret: String
bio: String
rooms: [Room]
createdAt: String
messages: [Message]
sender: [User]
receiver: [User]
}
I read the answers to other similar questions, but most said that I should fit the data type or asynchronous processing.
(Why does a GraphQL query return null?)
But my code used code for asynchronous processing, and I guess I matched the data type. Why does this code always return null?
Also, in addition to this Mutation, all other Query, Mutation and Subscriptions return nulls
createAccount.jsis being imported and how you're creating your resolver map using it. - Daniel ReardenmergeResolversto return a function instead of an object. - Daniel Rearden