我正在學習 GraphQL,但在嘗試啟動服務器時遇到了錯誤。
const graphql = require('graphql');
const _ = require('lodash');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema
} = graphql;
const Users = [
{ id: '23', firstName: 'Bill', age: 20 },
{ id: '41', firstName: 'Jane', age: 18 }
];
const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: GraphQLString },
firstName: { type: GraphQLString },
age: { type: GraphQLInt }
}
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: {
type: UserType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return _.find(Users, { id: args.id });
}
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
server.js 代碼在這里:
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const schema = require('./schema/schema');
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}));
app.listen(4000, () => {
console.log("Server running at port 4000.");
});
該錯誤似乎來自“module.exports”部分,因為當我將其注釋掉時,服務器啟動時沒有問題。
uj5u.com熱心網友回復:
存在語法混亂。在 RootQuery 中 user 下的 type 部分完全覆寫了整個欄位結構,錯誤在這里。
只需洗掉頂層type: {}。試試這個:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return _.find(Users, { id: args.id });
}
}
}
});
uj5u.com熱心網友回復:
謝謝,解決方案幫助了我。現在很有意義。??
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/428884.html
標籤:javascript 节点.js 表示 图形
