{"size": 1250, "ext": "js", "max_stars_repo_path": "11-GraphQL/code/baseType.js", "max_stars_repo_name": "WuChenDi/KnowledgePoint", "max_stars_repo_stars_event_min_datetime": "2018-07-27T07:53:21.000Z", "max_stars_repo_stars_event_max_datetime": "2018-11-14T01:07:02.000Z", "max_issues_repo_path": "11-GraphQL/code/baseType.js", "max_issues_repo_name": "WuChenDi/KnowledgePoint", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "11-GraphQL/code/baseType.js", "max_forks_repo_name": "WuChenDi/KnowledgePoint", "max_forks_repo_forks_event_min_datetime": "2018-08-23T01:28:18.000Z", "max_forks_repo_forks_event_max_datetime": "2018-11-14T01:05:39.000Z"}
{"max_stars_count": 5, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 18.115942029, "max_line_length": 76, "alphanum_fraction": 0.5776}
const express = require("express");
const { buildSchema } = require("graphql");
const { graphqlHTTP } = require("express-graphql");

// 定义schema 查询和类型
/**
 * 传输传递
 * 与 js 传递参数一样，小括号内定义行参，但是注意：传输需要定义类型
 * !() 代表传输不能为空
 * type Query {
 *  rollDice(numDice: Int!, numSides: Int): [Int]
 * }
 */
const schema = buildSchema(`
  type Account {
    name: String
    age: Int
    sex: String
    department: String
    salary(city: String): Int
  }
  type Query {
    getClassMates(classNo: Int!): [String]
    account(username: String): Account
  }
`);

const root = {
	getClassMates: ({ classNo }) => {
		const obj = {
			31: ["张三", "李四", "赵五"],
			61: ["张小三", "李小四", "赵小五"],
		};
		return obj[classNo];
	},
	account: ({ username }) => {
		const name = username;
		const sex = "boy";
		const age = 25;
		const department = "technical division";
		const salary = ({ city }) => {
			if (city === "北京" || city === "上海" || city === "广州") {
				return 10000;
			}
			return 3000;
		};
		return {
			name,
			age,
			sex,
			department,
			salary,
		};
	},
};

const app = express();

app.use(
	"/graphql",
	graphqlHTTP({
		schema: schema,
		rootValue: root,
		graphiql: true,
	})
);

app.listen(3000, () => console.log("Now browse to localhost:3000/graphql"));
