Apollo-client: Cannot return null for non-nullable field Post.author.

Created on 26 Nov 2018  路  6Comments  路  Source: apollographql/apollo-client

Need help or want to talk all things Apollo Client? Issues here are reserved for bugs, but one of the following resources should help:

import { GraphQLServer } from 'graphql-yoga'

// Scalar types - String, Boolean, Int, Float, ID

// Demo user data
const users = [{
id: '1',
name: 'Jonh',
email: '[email protected]',
age: 27
}, {
id: '2',
name: 'Sarah',
email: '[email protected]'
}, {
id: '3',
name: 'Mike',
email: '[email protected]'
}]

const posts = [{
id: '10',
title: 'art 100',
body: 'This is...',
published: true,
author: '1'
}, {
id: '11',
title: 'art 102',
body: 'This is a...',
published: false,
author: '1'
}, {
id: '12',
title: 'Programming Music',
body: '',
published: false,
author: '2'
}]

// Type definitions (schema)
const typeDefs = `
type Query {
users(query: String): [User!]!
posts(query: String): [Post!]!
me: User!
post: Post!
}

type User {
    id: ID!
    name: String!
    email: String!
    age: Int
}

type Post {
    id: ID!
    title: String!
    body: String!
    published: Boolean!
    author: User!
}

`

// Resolvers
const resolvers = {
Query: {
users(parent, args, ctx, info) {
if (!args.query) {
return users
}

        return users.filter((user) => {
            return user.name.toLowerCase().includes(args.query.toLowerCase())
        })
    },
    posts(parent, args, ctx, info) {
        if (!args.query) {
            return posts
        }

        return posts.filter((post) => {
            const isTitleMatch = post.title.toLowerCase().includes(args.query.toLowerCase())
            const isBodyMatch = post.body.toLowerCase().includes(args.query.toLowerCase())
            return isTitleMatch || isBodyMatch
        })
    },
    me() {
        return {
            id: '123098',
            name: 'Mike',
            email: '[email protected]'
        }
    },
    post() {
        return {
            id: '02',
            title: 'graph ....',
            body: '',
            published: false
        }
    }
},
Post: {
    author(parent, args, ctx, info) {
        return users.find((user) => {
            return user.id === parent.author
        })
    }
}

}

const server = new GraphQLServer({
typeDefs,
resolvers
})

server.start(() => {
console.log('..........')
})

Most helpful comment

Hi @lephenix1234 I ran into this post when facing the same issue.

Solution is simple. Just remove the exclamation mark form the Author property. That allows Graphql to send the value as null but the query response works as intended.

Hope it helps

All 6 comments

Check post() resolver. It doesn't have author field in return object. It should include author data not null.

// resolver
post() {
  return {
    id: '02',
    title: 'graph ....',
    body: '',
    published: false
    // no author value
  }
}

Hi @lephenix1234 I ran into this post when facing the same issue.

Solution is simple. Just remove the exclamation mark form the Author property. That allows Graphql to send the value as null but the query response works as intended.

Hope it helps

Why is it that for iOS things work and for Android, it breaks?

For me the issue was two-fold. I was attempting to query based on a GSI of a DDB I had created in the manner described here: https://medium.com/@ashleywnj/appsync-resolvers-dynamodb-queries-570ea33526b5
(think: query all postIDs associated with a particular userID (GSI)

First, I had to change my types because the DynamoDB Query option returns data in a particular format (this format is utilized in this answer https://stackoverflow.com/questions/52077167/appsync-query-on-global-secondary-index)

Secondly the query will require you to have your query fields organized in the following manner in order to display correctly:

items {    
  idDriver
  status
  lastLat
  lastLng 
}

@dhavaljardosh bad programming

Here is what worked out for me.

import { prop, Typegoose } from "@hasezoey/typegoose";
import { ObjectType, Field, ID } from "type-graphql";
import { ObjectId } from "mongodb";

@ObjectType()
export class TestClass extends Typegoose {
  @prop()
  @Field((_type) => String, { nullable: true })
  nullableField!: string;
Was this page helpful?
0 / 5 - 0 ratings