In the schema basics topic we defined the next schema structure:
const typeDefs = gql`
type Book {
title: String
author: Author
}
type Author {
name: String
books: [Book]
}
type Query {
getBooks: [Book]
getAuthors: [Author]
}
type Mutation {
addBook(title: String, author: String): Book
}
`;
But, with that structure and this data (provided by the documentation):
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
Is not possible to obtain the data as the documentation displays.
Doc result:
{
"data": {
"getBooks": [
{
"title": "Jurassic Park",
"author": {
"name": "Michael Crichton"
}
},
...
]
}
}
My result:
{
"data": {
"getBooks": [
{
"title": "Harry Potter and the Chamber of Secrets",
"author": {
"name": null
}
},
{
"title": "Jurassic Park",
"author": {
"name": null
}
}
]
}
}
am I doing something wrong or is it a doc error?
The schema basics article doesn't actually talk about implementing resolvers. Where did your implementation come from, how did you get there and what does it look like?
Take a look at the docs: Schema basics. I am following it step-by-step.
@enbermudas Yes, I took a look at the documentation and that is the documentation I am referring to. There is no resolver implementation that provides data on that page (or literally any JavaScript or any programming language that provides an implementation — only schema definition language) on that page, purely theories and concepts.
Further, there seem to be no explicitly actionable steps to follow as you are describing. Can you please share your implementation as I requested above to help unwind the confusion? Not answering questions won't help us resolve the confusion!
@enbermudas Hope you are mixing the contents from getting-started and schema-basics. My suggestion is if you follow the getting-started first, then you can solve the issue yourself.
FYI,
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
this is from getting-started, but it won't work with queries that you have mentioned in this issue. This data is not enough to work with your problem, you need to add some more data to work with your problem.
@abernix @enbermudas Please excuse me if I'm wrong.
That's what I'm talking about: there is code missing and there is no explanation on how to generate that code between those to tutorials. I'm lost.
That's what I'm talking about: there is code missing and there is no explanation on how to generate that code between those to tutorials. I'm lost.
@enbermudas Understood. But the getting-started is an overall idea about how to develop a server (create a project) and the schema-basics is just schema basics, so if you want to define data for the schema do it yourself, because we can do it in different ways. So that decision is always up to you. And I hope once you covered all the topics you will get the answer for your question. Now let me show you a few examples:
example 1
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: {
name: 'J.K. Rowling'
}
},
{
title: 'Jurassic Park',
author: {
name: 'Michael Crichton'
}
},
];
example 1
const books = [
{
authorId: 1
title: 'Harry Potter and the Chamber of Secrets',
},
{
authorId: 2
title: 'Jurassic Park',
},
];
const authors = [
{
id: 1
name: 'J.K. Rowling',
},
{
id: 2
name: 'Michael Crichton',
},
];
We can define data for the schema in different ways as I stated above. Hope this will help you.
Correct me if I'm wrong
So, Does anybody know the correct answer for this part. I got the same problem now which got the out put as author:Null.
So, Does anybody know the correct answer for this part. I got the same problem now which got the out put as author:Null.
@YuzhongPan Is there any way to see your code, then we can find the exact reason?
So, Does anybody know the correct answer for this part. I got the same problem now which got the out put as author:Null.
@YuzhongPan Is there any way to see your code, then we can find the exact reason?
Here is part of index.js:
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
},
{
title: 'Jurassic Park',
},
];
const authors = [
{
name: 'J.K. Rowling',
},
{
name: 'Michael Crichton',
},
];
and this is the output of query:
{
"data": {
"books": [
{
"title": "Harry Potter and the Chamber of Secrets",
"author": null
},
{
"title": "Jurassic Park",
"author": null
}
]
}
}
and the correct output in documentation is:
{
"data": {
"books": [
{
"title": "Jurassic Park",
"author": {
"name": "Michael Crichton"
}
},
...
]
}
}
and the query is:
query GetBooks {
books {
title
author {
name
}
}
}
I am confused why it is null and how to get the output for 'author' from the 'the name of authors'
Replace example from page https://www.apollographql.com/docs/apollo-server/getting-started/#step-4-define-your-data-set
to this
const authors = [
{
name: 'J.K. Rowling',
},
{
name: 'Michael Crichton',
},
]
const books = [
{
author: authors[0],
title: 'Harry Potter and the Chamber of Secrets',
},
{
author: authors[1],
title: 'Jurassic Park',
},
]
const resolvers = {
Query: {
books: () => books,
authors: () => authors,
},
}
(thanks @nikhilunni511)
And it works!

PS: For "Mutation" section is a bit harder to provide a real code.
I'm still a bit confused as to what the complaint is here.
If I understand correctly, this page https://www.apollographql.com/docs/apollo-server/getting-started/
has Book.author: String and is self-consistent.
Then this page https://www.apollographql.com/docs/apollo-server/schema/schema/
has an example with Book.author: Author. It does not attempt to show a fully runnable program at all; it just has some examples of things you can do with a schema. No resolvers involved.
Is the concern that the schema basics page doesn't talk about resolvers? The structure of the docs are currently that this section is just about your schema, and you wait until the "Fetching Data" section to talk about resolvers etc.
Is the concern that two pages have two examples that are similar but not identical, and that that is confusing?
I can reopen this if I understand more specifically what the problem is.
Most helpful comment
@enbermudas Hope you are mixing the contents from getting-started and schema-basics. My suggestion is if you follow the getting-started first, then you can solve the issue yourself.
FYI,
const books = [ { title: 'Harry Potter and the Chamber of Secrets', author: 'J.K. Rowling', }, { title: 'Jurassic Park', author: 'Michael Crichton', }, ];this is from getting-started, but it won't work with queries that you have mentioned in this issue. This data is not enough to work with your problem, you need to add some more data to work with your problem.
@abernix @enbermudas Please excuse me if I'm wrong.