accounts icon indicating copy to clipboard operation
accounts copied to clipboard

Typeorm/Graphql: How to retrieve profile information (eg firstname/lastname) and where to store it

Open derblitz opened this issue 5 years ago • 1 comments

If using the createUser function on the frontend in order to create a user and when having included a profile (like in the official example), I'm wondering where this information is saved on the backend? Or if it's not saved, how I can retrieve it and save it manually? I'm using postgres and don't see the information stored in any of the tables.

Frontend code:

public onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    this.setState({ error: null });
    try {
        await accountsPassword.createUser({
            email: this.state.email,
            username: this.state.email,
            password: this.state.password,
            profile: {
                firstName: this.state.firstName,
                lastName: this.state.lastName,
            },
        })
        this.props.history.push('/login/');
    } catch (err) {
        console.log(err)
        this.setState({ error: err.message })
    }
}

Backend (typeDefs):

extend input CreateUserInput {
    profile: CreateUserProfileInput!
}

input CreateUserProfileInput {
    firstName: String!
    lastName: String!
}

derblitz avatar Aug 08 '19 21:08 derblitz

Hi, I'm facing the same confusion. Any guidance available on this?

stevenmasci avatar Jun 14 '21 06:06 stevenmasci

Where it's stored depended on the database adapter. All the existing database adapters store the additional fields together with the user. Check 1.0 examples, we don't have profile anymore but we simply extend CreateUserInput with firstName and lastName.

darkbasic avatar Nov 22 '23 21:11 darkbasic

Anyway the problem with the typeorm adapter is that it has standard entities which you cannot extend and in order to add additional fileds it does the following:

Object.assign(user, otherFields);

await this.userRepository.save(user);

How that works is black magic to me, but I admit I don't user typeorm since a long time.

The new MikroORM database adapter is much better in that regard because it allows you to extend the base entities any way you want. I don't plan to improve the typeorm adapter because typeorm's pace of development has basically stalled and mikro-orm is much better in my opinion nowadays.

darkbasic avatar Nov 22 '23 21:11 darkbasic

Apparently it might be possible to extend the User entity, but that's not the approach that they've taken in the example. Feel free to try it.

darkbasic avatar Nov 22 '23 21:11 darkbasic

How that works is black magic to me, but I admit I don't user typeorm since a long time.

Nevermind, apparently it doesn't work at all.

darkbasic avatar Nov 22 '23 21:11 darkbasic