Я изучаю GraphQL и не могу понять, почему name
и description
этого запроса возвращают нулевые данные? Я делаю что-то не так или, может быть, где-то опечатка?
дБ. json (использование json-server
для временного хранения данных бэкэнда)
{
"users": [
{
"id": "23",
"firstName": "Chun-li",
"age": 32,
"companyId": "1"
},
{
"id": "40",
"firstName": "E.Honda",
"age": 45,
"companyId": "1"
},
{
"id": "1",
"firstName": "Sagat",
"age": 45,
"companyId": "2"
}
],
"companies": [
{
"id": "1",
"name": "Apple",
"description": "iphone"
},
{
"id": "2",
"name": "Google",
"description": "search"
}
]
}
сервер. js (использование промежуточного программного обеспечения express)
import express from 'express'
import expressGraphQL from 'express-graphql'
import {
schema
} from './schema/schema'
const app = express()
app.use('/graphql', expressGraphQL({
schema,
graphiql: true
}))
app.listen(4000, () => {
console.log('server is listening')
})
схема. js
import axios from 'axios'
import {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
} from 'graphql'
const CompanyType = new GraphQLObjectType({
name: 'Company',
fields: {
id: {
type: GraphQLString
},
name: {
type: GraphQLString
},
description: {
type: GraphQLString
}
}
})
const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: {
type: GraphQLString
},
firstName: {
type: GraphQLString
},
age: {
type: GraphQLInt
},
company: {
type: CompanyType,
async resolve(parentValue, args) {
console.log({
parentValue,
args
})
try {
const response = await axios.get(`http://localhost:3000/users/${parentValue.companyId}`)
console.log(response)
return response.data
} catch (error) {
return console.log(error)
}
}
}
}
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: {
id: {
type: GraphQLString
}
},
async resolve(parentValue, args) {
try {
const response = await axios.get(`http://localhost:3000/users/${args.id}`)
return response.data
} catch (error) {
return console.log(error)
}
}
}
}
})
export const schema = new GraphQLSchema({
query: RootQuery
})
Мой запрос;