我以编程方式创建了一个模拟来测试我的查询,但它只返回一个空值我做错了什么。我正在使用休闲库为我的模拟生成值
这是我的文件:
index.js
import express from 'express'
import bodyParser from 'body-parser'
import { graphqlExpress,graphiqlExpress } from 'graphql-server-express'
import { makeExecutableSchema } from 'graphql-tools'
import mock from './mock'
import schema from './schema'
const app = express()
const executableSchema = makeExecutableSchema({
typeDefs: schema,
mock
})
app.post(
'/graphql',
bodyParser.json(),
graphqlExpress(() => ({
schema: executableSchema
}))
)
app.use('/graphiql',graphiqlExpress({
endpointURL: '/graphql',
}))
app.listen(8080)
console.log("listening at 8080")
schema.js
const schema = `
interface Novel {
artist: String
author: String
chapters: [Chapter]
description: String
genre: [String]
id: String
image: String
language: String
modified: String!
name: String!
status: String
tags: [String]
type: String
year: String
}
interface Chapter {
chapter: String!
volume: String!
content: String!
}
type NovelCrawler implements Novel{
artist: String
author: String
chapters: [Chapter]
description: String
genre: [String]
id: String
image: String
language: String
modified: String!
name: String!
novelurl: String
status: String
tags: [String]
type: String
year: String
}
type ChapterCrawler implements Chapter {
chapter: String!
chapterurl: String
content: String!
novelurl: String
volume: String!
}
`
const Query = `
type Query {
novels: [Novel]
}
`
export default [schema, Query]
模拟.js
import casual from 'casual'
const genre = ['romance', 'comedy', 'action']
const Novel = () => ({
artist: casual.name,
author: casual.name,
description: casual.sentences(3),
genre: [casual.random_element(genre), casual.random_element(genre)],
id: casual.url,
image: 'sdfgrtw3wedfgb2345y675trfdvfbghdsfdssfd',
language: casual.random_element(['chinese', 'japanese', 'korean']),
modified: casual.date('YYYY-MM-DD'),
name: casual.word,
status: casual.random_element(['Completed', 'Ongoing']),
tags: [],
novelurl: casual.url,
type: casual.random_element(['Web Novel', 'Light Novel']),
year: casual.year
})
const Chapter = () => ({
chapter: casual.integer(1, 50),
chapterurl: casual.url,
content: casual.sentences(10),
novelurl: casual.url,
volume: casual.integer(1, 5)
})
const arrNovel = []
const arrChapter = []
for (let x = 0; x < 20; ++x) {
arrNovel.push(Novel())
arrChapter.push(Chapter())
}
const mock = {
// Query: { novels: () => ({}) },
Novel: {
__resolveType(obj, context, info) {
return 'NovelCrawler'
}
},
Chapter: {
__resolveType(obj, context, info) {
return 'ChapterCrawler'
}
},
NovelCrawler: () => arrNovel,
ChapterCrawler: () => arrChapter
}
export default mock
当我运行以下查询时:
{
novels{
description
}
}
它返回:
{
"data": {
"novels": null
}
}
所以我尝试了:
{
novels{
... on NovelCrawler{
artist
}
}
}
这返回
{
"errors": [
{
"message": "Unknown operation named \"null\"."
}
]
}
我究竟做错了什么