Cannot return null for non-nullable field Post.author.
See original GitHub issueNeed help or want to talk all things Apollo Client? Issues here are reserved for bugs, but one of the following resources should help:
- Apollo’s Slack: https://www.apollographql.com/slack
- StackOverflow (
apollo-client
tag): https://stackoverflow.com/questions/tagged/apollo-client - Apollo Feature Request repo: https://github.com/apollographql/apollo-feature-requests
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:
- Created 5 years ago
- Comments:6
Top GitHub Comments
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?