阅读背景:

express.js graphql express-graphql

来源:互联网 

文档

创建应用

const l = console.log;
var express = require("express");
var graphqlHTTP = require("express-graphql");
var { buildSchema } = require("graphql");

const cats = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }];

var schema = buildSchema(`
  type Query {

    hello: String

    cats: [Cat]

    findCat(id: ID!): Cat
  }

  type Cat {
    id: Int
    name: String
  }
`);

// root 提供所有 API 入口端点相应的解析器函数
var root = {
  hello: () => "Hello world 233!",
  cats: () => cats,
  findCat({id}) {
    return cats.find(el => el.id === Number(id));
  }
};

var app = express();

// 根节点
app.use(
  "/graphql",
  graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true
  })
);
app.listen(4000);const l = console.log;
var express =



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: