I'm trying to set up my schema for Apollo Server, and I'm running into an error. I'm not really sure how to put this question so I will post text/code than necessary.
"message": "Cannot return null for non-nullable field User.id."
My resolvers/index.js look like this:
import * as Mutation from "./Mutation";
import * as Query from "./Query";
import User from "./User";
const resolvers = { Mutation, Query, User };
console.log(resolvers);
export default resolvers;
Which produces the following output:
{
Mutation: {
createListing: [Getter],
updateListing: [Getter],
deleteListing: [Getter],
createUser: [Getter]
},
Query: { listings: [Getter], listing: [Getter], userLogin: [Getter] },
User: [AsyncFunction: user]
}
My typeDefs file looks like this
import { gql } from "apollo-server";
const typeDefs = gql`
type Listing {
description: String!
id: ID!
title: String!
createdBy: User!
}
type User {
id: ID!
email: String!
}
type AuthData {
id: ID!
token: String!
expiresIn: String!
}
input UserInput {
email: String!
password: String!
}
input ListingInput {
id: ID
title: String!
description: String!
}
type Mutation {
createUser(userInput: UserInput): User!
createListing(listingInput: ListingInput): Listing!
updateListing(listingInput: ListingInput): Listing!
deleteListing(id: ID!): Boolean!
}
type Query {
listings: [Listing!]!
listing(id: ID!): Listing!
userLogin(userInput: UserInput): AuthData!
}
`;
export default typeDefs;
The problem is I don't know how get User data on the Listing Type
createdBy is actually ID! field and I'd like graphQL to get User data for me. Below is the query I've been trying to use.
query {
listing(id: 35)
{
id
title
createdBy {
id
email
}
}
}
Graphql beginner here. Am I using the right approach? I can also provide the git repo, if needed. How do I reorganize this so it works?
listing(id)
resolver ? doesn't return user type forcreatedBy
- probably returns only userid
from DB listing record .. use join – xadm