Dart: 请求graphql数据

import 'package:http/http.dart' as http;
const url = "http://127.0.0.1:4000/graphql";
main(List<String> args) async {
  cat("b");
}

cat(String name) async {
  String query = '''
  query {
    cat(name: "$name") {
      name 
      age
    }
   }
  ''';
  String u = "${url}?query=${query}";
  var r = await http.post(u);
  print(r.body);
}
const l = console.log;
var express = require('express');
var graphqlHTTP = require('express-graphql');
var {buildSchema} = require('graphql');

const cats = [{name: 'a', age: 12}, {name: 'b', age: 13}, {name: 'c', age: 14}];

var schema = buildSchema(`
  type Query {
    cat(name: String): Cat
  }

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

var root = {
  cat({name}) {
    return cats.find(el => el.name === name.trim());
  },
};

var app = express();

app.use(
  '/graphql',
  graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true,
  }),
);
app.listen(4000);
posted @ 2019-06-04 12:04  Ajanuw  阅读(220)  评论(0编辑  收藏  举报