graphql error: Cannot return null for non-nullable field when calling field in query

graphql error: Cannot return null for non-nullable field when calling field in query


0

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. */
  @OneToOne(() => MemberType)
  @JoinColumn()
  @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. */
  @Column({ unique: true })
  @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);
    console.log('memberType = ', memberType); // this prints out the correct into
    console.log('member id = ', memberType.id); // this also prints out the corrent info (1)
    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 });
    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-07T07:40:03.513Z,
  firstName: 'foob',
  githubUrl: null,
  id: '48b76f72-1348-4708-8d09-471cc82def13',
  lastName: 'bar',
  linkedinUrl: null,
  personalUrl: null,
  photoUrl: 'testingPhoto',
  updateAt: 2023-10-07T08:49:00.633Z
}

and here is the memberList[0]:

Member {
  createdAt: 2023-10-07T07:40:03.513Z,
  firstName: 'foob',
  githubUrl: null,
  id: '48b76f72-1348-4708-8d09-471cc82def13',
  lastName: 'bar',
  linkedinUrl: null,
  personalUrl: null,
  photoUrl: 'testingPhoto',
  updateAt: 2023-10-07T08:50:00.103Z
}

New contributor

soulew is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

  • Can you post content updatedMember or memberList?

    – Sagar Darekar

    34 mins ago


  • Field memberType is marked as not null in your case, so you need to make sure non nullable value is present for each member for that specific field.

    – Sagar Darekar

    29 mins ago


  • @SagarDarekar I updated the post to include them

    – soulew

    22 mins ago



Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *