您好,欢迎来到外链网!
当前位置:外链网 » 站长资讯 » 专业问答 » 文章详细 订阅RssFeed

graphql sql,graphql mysql最佳实践

来源:互联网 浏览:107次 时间:2023-04-08
GraphQL以及Apollo-GraphQL的使用(一)

GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。

一、GraphQL的基本使用 1.安装 npm install graphql 2.引入 const { graphql, buildSchema } = require('graphql') 3.声明graphql的架构图/概要 let schema = buildSchema(` type Query{ name:String }`) 4.解析器 const root = { hello: ()=> 'Hello world!'} 5.创建graphql服务器 graphql( schema, '{hello}', root).then((response) 美国高防vps => { console.log(response); //{ hello: 'Hello world!' }}) 二、Apollo-GraphQL在express框架中的使用

Apollo服务器是一种符合规范的开源GraphQL服务器,与任何GraphQL客户端(包括[Apollo Client)兼容。

1.安装 express中的Apollo依赖 npm install --save graphql apollo-server-express 2.导入模块 import {ApolloServer,gql} from 'apollo-server-express' 3.声明schema"架构图" const typeDefs = gql( ` type Query{ hello:String, } `) 4.声明解析器 const resolvers = { Query:{ hello: ()=>"hello world", }} 5.创建graphql server const server = new ApolloServer({ typeDefs, // schema“架构图/概要” resolvers, // 解析器 playground:true, //true-启动自带的图形化服务 false-不启动自带的图形化服务}) 6、将graphql服务器嵌入到express中 const app = express()server.applyMiddleware({ app, path:"/gql" //访问自带的图形化服务的路径})

链接: 1.gql的中文网站
2.gql官网

22233644