极客时间对于推广渠道会有返利优惠,比如山月在极客时间买了一门课,再把课程分享给好友购买,这时极客时间会向山月返利20元左右。
而我现在做了一个返利平台,你可以在上边通过山月的链接购买课程,此时极客时间会向我返利。为了共同学习,而你可以添加我的微信 (shanyue94),我将把极客时间给我的返利发一个红包全部返给你

# hello, world

我们先写一个关于 graphql.jshello, world 示例,并且围绕它展开对 graphql 的学习

import { graphql, GraphQLSchema, GraphQLObjectType, GraphQLString } from 'graphql'


// `graphql.js` 可以视作由两大部分组成

// 1. `query`,可以视作 rest 中的 API,它可以与客户端相结合,提供查询语句
// 1. `schema`,可以视作 rest 中对应 API 的逻辑层,它可以与服务端相结合,根据查询语句提供结果

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'hello, shanyue'
        }
      }
    }
  })
})

const query = '{ hello }'

graphql({
  schema,
  source: query
}).then(result => {
  // { hello: "hello, shanyue" }
  console.log(result)
})

由上,也可以看出 graphql 很关键的两个要素:schemaquery。而当我们开发 web 应用时,schema 将会是服务端的主体,而 query 存在于前端中,类似 REST 中的 API。

# Schema

import { graphql, buildSchema } from 'graphql'

const schema = buildSchema(`
  type Query {
    hello: String
  }
`)

const root = {
  hello (...args) {
    return 'hello, shanyue'
  }
}

const query = '{ hello }'

graphql({
  schema,
  source: query,
  rootValue: root
}).then((result) => {
  console.log(result)
})

# Schema With Resolvers

关于山月

我的项目:
我的微信:shanyue94,欢迎交流
Last Updated: 2/27/2022, 7:45:54 PM