type XXX
type Book {
id: ID
title: String
author: Author // 自定义类型
summary:String
isbn: Int
test: Boolean
}
type Query
type Query {
// api名称(入参):返回类型
findBook(id: String): Book
findAuthor(id: String): Author
}
type Mutation
type Mutation {
// API 名称(入参):返回值
createAuthor(input: AuthorInput!): Author!
}
input XXX
input BookInput {
title: String
author: AuthorInput
}
input AuthorInput{
first_name: String
family_name: String
}
// api 对应的具体实现方法的对象
var root = {
hello: () => 'Hello world!',
hello2: () => 'hello 2'
};
app.use('/graphql', graphqlHTTP({
schema: schema, // 构建好的schema
rootValue: root, // api对应的具体实现方法的对象
graphiql: true, // graphql的开发调试页面
}));
// 引入一些依赖
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
// 构建 schema
var schema = buildSchema(`
type Query {
hello: String
hello2: String
}
`);
// api 对应的具体实现方法的对象
// 方法名(入参)返回值与 type Query 中定义的保持一致
var root = {
hello: () => 'Hello world!',
hello2: () => 'hello 2'
};
var app = express();
// express-graphql 使用
app.use('/graphql', graphqlHTTP({
schema: schema, // 构建好的 schema
rootValue: root, // api 对应的具体实现方法的对象
graphiql: true, // graphql 的开发调试页面
}));
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));