服务器安装实施
GraphQL.js
GraphQL.js 是 GraphQL 的 JavaScript 参考实现。你可以通过 npm 安装它:
- 如果你还没有在项目中初始化 npm:
npm init
- 从 npm:
npm install --save graphql
安装 GraphQL.js
示例服务器
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return 'Hello world!';
},
};
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
console.log(response);
});