I'm trying to query data in GraphQL and express. I wrote the schema and the resolvers, but no matter what I do, it keeps returning null. I tried to re-write everything, I tried to use apollo-express, I looked everywhere but no matter what I tried it kept returning null.
const express = require("express");
const app = express();
const port = process.env.PORT || 8080;
const { graphqlHTTP } = require("express-graphql");
const { buildSchema } = require("graphql");
const cors = require("cors");
let books = [
{ name: "Name of the Wind", genre: "Fantasy", id: "1", authorId: "1" },
{ name: "The Final Empire", genre: "Fantasy", id: "2", authorId: "2" },
{ name: "The Hero of Ages", genre: "Fantasy", id: "4", authorId: "2" },
{ name: "The Long Earth", genre: "Sci-Fi", id: "3", authorId: "3" },
{ name: "The Colour of Magic", genre: "Fantasy", id: "5", authorId: "3" },
{ name: "The Light Fantastic", genre: "Fantasy", id: "6", authorId: "3" },
];
const Schema = buildSchema(`
type Query {
book(id: ID!): Book!
books: [Book!]
test: String
}
type Book {
id: ID!
name: String!
genre: String!
author: Author!
},
type Author {
id: ID!
name: String!
age: Int!
books: [Book]!
}
`);
let root = {
Query: {
books: () => {
return books;
},
book: (parent: any, args: any) => {
Book.findById(args.id);
},
},
};
app.use(
"/graphql",
cors(),
graphqlHTTP({
schema: Schema, // Must be provided
rootValue: root,
graphiql: true,
})
);
Here is the query:
{
books {
name
id
}
}
And here is the query response:
{
"data": {
"books": null
}
}
Hope it's enough.
I was able to get your query to work in apollo server by excluding the below lines. Was the intention to apply findById to books?
book: (parent: any, args: any) => {
Book.findById(args.id);
}
See below:
const { ApolloServer, gql } = require("apollo-server");
let books = [
{ name: "Name of the Wind", genre: "Fantasy", id: "1", authorId: "1" },
{ name: "The Final Empire", genre: "Fantasy", id: "2", authorId: "2" },
{ name: "The Hero of Ages", genre: "Fantasy", id: "4", authorId: "2" },
{ name: "The Long Earth", genre: "Sci-Fi", id: "3", authorId: "3" },
{ name: "The Colour of Magic", genre: "Fantasy", id: "5", authorId: "3" },
{ name: "The Light Fantastic", genre: "Fantasy", id: "6", authorId: "3" }
];
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
book(id: ID!): Book!
books: [Book!]
test: String
}
type Book {
id: ID!
name: String!
genre: String!
author: Author!
}
type Author {
id: ID!
name: String!
age: Int!
books: [Book]!
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
books: () => {
return books;
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Just to note: your books and Book type definition don't match (authorId vs author).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.