【发布时间】:2024-04-19 10:55:02
【问题描述】:
我是 GraphQL 的新手。作为实验,我运行了以下 Node.js Express 服务器:
var express = require('express');
var { graphqlHTTP } = require('express-graphql');
var { buildSchema } = require('graphql');
var schema = buildSchema(`
type Student {
sid: ID,
name: String,
subjects: [String!],
address: Location
}
type Location{
unit: String,
city: String,
country: String,
postCode: Int
}
`);
var root = { // The root provides a resolver function for each API endpoint
address: () => ({
city: ()=>'London'
}),
};
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
当我在浏览器的界面上运行以下查询时:
{
address {
city
}
}
我遇到了一个错误:
{
"errors": [
{
"message": "Query root type must be provided."
}
]
}
这样做的正确方法应该是什么?
【问题讨论】:
标签: node.js express schema definition graphql-js