question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot return null for non-nullable field Post.author.

See original GitHub issue

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: ‘john@example.com’, age: 27 }, { id: ‘2’, name: ‘Sarah’, email: ‘sarah@example.com’ }, { id: ‘3’, name: ‘Mike’, email: ‘mike@example.com’ }]

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: 'mike@example.com'
        }
    },
    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(‘…’) })

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

141reactions
kjimenezdevcommented, May 19, 2019

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

34reactions
dhavaljardoshcommented, Nov 12, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am I getting a "Cannot return null for non-nullable field ...
The biggest problem with your resolver is that in any number of scenarios, instead of returning a User object, you return a string....
Read more >
`Cannot return null for non-nullable field` when throwing error
When querying me without ctx.user set, it returns 2 errors instead of 1. Errors: Error: MISSING_AUTHENTICATION Cannot return null for non- ...
Read more >
Cannot return null for non-nullable type graphql - Fauna Forums
I get the following error when i query allTodos index through graphql "errors": [ { "message": "Cannot return null for non-nullable type ...
Read more >
"Cannot return null for non-nullable field" error - GraphQL API
I have an Item custom class, and I use a custom graphql mutation to update an item via a resolver in Cloud code...
Read more >
cannot return null for non-nullable field - You.com | The AI ...
The biggest problem with your resolver is that in any number of scenarios, instead of returning a User object, you return a string....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found