I am getting this error when I try to query my memberList resolver, It needs to return a membeTypeID but it is returning null - i am using Apollo for this if that helps at all:
"errors": [
{
"message": "Cannot return null for non-nullable field Member.memberType.",
"locations": [
{
"line": 5,
"column": 3
}
],
"path": [
"memberList",
0,
"memberType"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"stacktrace": [
"Error: Cannot return null for non-nullable field Member.memberType.",
here is my query:
query MemberList {
memberList{
firstName
id
memberType{id}
}
}
here is a snippet of my Member Entity that uses typeORM & Graphql:
/** Reference to type of member. */
@ManyToOne(() => MemberType, (memberType) => memberType.memberType, { eager: true })
// @JoinColumn({referencedColumnName: 'testing'})
@Field(() => MemberType)
memberType!: MemberType;
here is a snippet of the member-type entity:
/** ID number of a member type. */
@PrimaryGeneratedColumn('increment')
@Field()
id!: number;
/** Type that a member can be. */
@OneToMany(() => Member, (member) => member.memberType)
@Field()
memberType!: string;
here is the memberList:
/** Get an array of all members */
@Query(() => [ Member ], { nullable: true })
async memberList(): Promise<Member[] | null>{
const memberList = await Member.find();
return memberList;
}
it throws the same error when I try to run the query for the following mutation as well:
/** Update a member with new details */
@Mutation(() => Member, {nullable: true })
async memberUpdate(
@Arg('input') input: MemberUpdateInput,
@Ctx() { request }: AppContext
): Promise<Member | null> {
input.throwIfInvalid();
const { userID } = request.session;
const existingUser = await User.findOneBy({ id: userID });
if (!existingUser) {
throw new AuthenticationError;
}
const existingMember = await Member.findOneBy({ id: input.id });
if (!existingMember) {
throw new FormError({
control: [ `Member '${input.firstName} ${input.lastName}' does not exist!` ]
});
}
const memberTypeInput = new IdNumberInput();
memberTypeInput.id = input.memberType.id;
const memberType = await new MemberTypeResolver().memberTypeDetails(memberTypeInput);
await Member.update({ id: input.id }, {
firstName: input.firstName,
githubUrl: input.githubUrl,
lastName: input.lastName,
linkedinUrl: input.linkedinUrl,
memberType: memberType,
personalUrl: input.personalUrl,
photoUrl: input.photoUrl
});
const updatedMember = await Member.findOneBy({ id: input.id });
Member.save;
console.log(updatedMember?.memberType);
if (!updatedMember) {
throw new ServerError;
}
return updatedMember;
}
here is the input I give
{
"input": {
"id": "48b76f72-1348-4708-8d09-471cc82def13",
"firstName": "foob",
"lastName": "bar",
"memberType": {
"id": 2
},
"photoUrl": "testingPhoto"
}
}
and here is the input type:
@InputType()
export class MemberUpdateInput extends UuidInput {
@Field()
firstName!: string;
@Field(() => String, { nullable: true })
githubUrl?: string;
@Field()
lastName!: string;
@Field(() => String, { nullable: true })
linkedinUrl?: string;
@Field(() => IdNumberInput)
memberType!: IdNumberInput;
@Field(() => String, { nullable: true })
personalUrl?: string;
@Field()
photoUrl!: string;
I've set the memberType to be optional just to see what it gives and it returns null, i also tried to make the memberType in the Member entity a number that is OneToOne with the table but that did not work either.
It looks like the memberType is not being pulled into the findbyOne or find, here is what updatedMember returns:
Member {
createdAt: 2023-10-07T09:56:27.589Z,
firstName: 'testin',
githubUrl: null,
id: '8c636b7b-4d83-4681-a912-87aee5a95c2e',
lastName: 'bar',
linkedinUrl: null,
personalUrl: null,
photoUrl: 'testThing',
updateAt: 2023-10-07T09:56:27.589Z,
memberType: MemberType {
createdAt: 2023-10-07T09:21:01.670Z,
id: 1,
updateAt: 2023-10-07T09:21:01.670Z
}
}
and here is the memberList[0]:
Member {
createdAt: 2023-10-07T09:56:27.589Z,
firstName: 'testin',
githubUrl: null,
id: '8c636b7b-4d83-4681-a912-87aee5a95c2e',
lastName: 'bar',
linkedinUrl: null,
personalUrl: null,
photoUrl: 'testThing',
updateAt: 2023-10-07T09:56:27.589Z,
memberType: MemberType {
createdAt: 2023-10-07T09:21:01.670Z,
id: 1,
updateAt: 2023-10-07T09:21:01.670Z
}
}
Alright, I put the OneToMany relationship on the wrong column in the member type entity:
here is the updated MemberType entity
and here is the Member entity memberType Column:
here is how I query the data now: