Search code examples
graphqlgraphql-codegenurql

Graphql Codegen specifying optional fields


Say I have the following fragment:

fragment ConversationSnippet on Conversation {
    uuid
    unreadMessages
    profileThatHasUnreadMessages
    updatedAt
    createdAt
    profiles{
        ...ConversationProfileSnippet
    }
    messages{
        ...MessageSnippet
    }
    ongoingCall
    pendingCall
    pendingCallProfile {
        ...ConversationProfileSnippet
    }
}

The pendingCallProfile could be null null. Is there a way for me to specify that in the fragment? I'm facing an issue where if the pendingCallProfile is null the entire query fails to return anything.


Solution

  • Turned out to be a simple solution, all I had to do was specify that the field was nullable on the Conversation entity server side.

      @Field(() => Profile, { nullable: true })
      @OneToOne(() => Profile, {
        nullable: true,
      })
      @JoinColumn()
      pendingCallProfile: Profile