Skip to content

Latest commit

 

History

History
136 lines (112 loc) · 2.41 KB

README_zh.md

File metadata and controls

136 lines (112 loc) · 2.41 KB

GraphQL-Server-Demo

readme

GraphQL-Server-Demo是一个帮助大家更好的学习和理解GraphQL的简单且易读的示例项目,通过nodejs创建服务

截屏

查询(Query)

变更(Mutation)

项目结构

GraphQL-Server-Demo
├── README.md
├── LICENSE
├── .babelrc
├── .gitignore
├── package.json
├── yarn.lock
├── dev.sqlite
├── app.js
├── schemas
│   └── index.js
├── resolvers
│   ├── userResolver.js
│   ├── messageResolver.js
│   └── ...
└── types
    ├── query.js
    ├── mutation.js
    ├── queries
    │    ├── userType.js
    │    ├── messageType.js
    │    └── ... 
    └── mutations
         ├── userInputType.js
         ├── messageInputType.js
         └── ... 
  • app.js -- 项目服务引擎
  • dev.sqlite -- 测试数据库
  • schemas -- 用来创建GraphQLSchema的基本schema文件, 包含最基本的queryTypemutationType
  • types -- 所有用户自定义的GraphQLObjectType
  • resolvers -- 所有类型字段的解析器

快速启动

输入以下命令:

git clone [email protected]:LanceGin/GraphQL-Server-Demo.git
cd GraphQL-Server-Demo
yarn && yarn start

打开url http://localhost:4000/graphql,窗口中会显示 GraphiQL GUI ,可以用来执行查询或者变更操作

查询操作

请求语句

query {
  user(id: 1) {
    name
    nickname
    message {
      id
      content
    }
  }
}

请求结果

{
  "data": {
    "user": [
      {
        "name": "gin",
        "nickname": "lancegin",
        "message": [
          {
            "id": "1",
            "content": "test message"
          },
          {
            "id": "2",
            "content": "hello"
          },
          {
            "id": "3",
            "content": "world"
          }
        ]
      }
    ]
  }
}

变更操作

请求语句

mutation {
  createMessage(input: {user_id: "1", content: "hello world111"}) {
    id
    content
  }
}

请求结果

{
  "data": {
    "createMessage": {
      "id": "10",
      "content": "hello world111"
    }
  }
}