1

My query for a single post shows

Cannot return null for non-nullable field Post.author

error message when I include the author field, both from React as well as the Playground. It's working fine for the posts query which queries for multiple posts and is able to retrieve the specific author, but not when I perform a single post query.

Client-side schema:

posts(query: String, first: Int, skip: Int, after: String, orderBy: UserOrderByInput): [Post!]!
post(id: ID): Post!

Query from React:

(gql is the same for post and posts except for the sorting arguments)

const { data, error, loading } = useQuery(GET_POST, { 
     variables: {
         id: props.match.params.id
     }
 })
export const GET_POST = gql`
    query Post($id: ID!) {
        post(
            id: $id
        ){
            id
            title
            body
            location
            author{
                id
                firstName
                lastName
            }
        }
    }
`

Server-side query:

 post(parent, args, { prisma }, info) {
        if(!args.id) {
            throw new Error("No search query input")
        }
        return prisma.query.post({
            where: {
                id: args.id
            }, info})
    },
Kevvv
  • 3,655
  • 10
  • 44
  • 90
  • If your posts query works why not make your single post prisma request to `posts` also and return the first result from the array. You'd need to async / await . – Andrew1325 May 19 '19 at 02:08
  • @Andrew1325 I'm currently using that method as a work around, but it's not ideal since it returns an array. Plus, I just fail to see why the above method isn't working. – Kevvv May 19 '19 at 03:44
  • I mean in your server side function use `prisma.query.posts`, there's really nothing wrong with it returning an array, you still use what it contains in the same way. Also `posts` allows a whole variety of `where` fields while `post` only allows you to search by id. As for why post doesn't work, it must be how your `datamodel.prisma` is setup. – Andrew1325 May 19 '19 at 07:28

0 Answers0