• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

matthewmueller/graph.ql: Faster and simpler way to create GraphQL servers

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

matthewmueller/graph.ql

开源软件地址(OpenSource Url):

https://github.com/matthewmueller/graph.ql

开源编程语言(OpenSource Language):

JavaScript 99.8%

开源软件介绍(OpenSource Introduction):

img

graph.ql

Faster and simpler technique for creating and querying GraphQL schemas.

Video Course

If you're interested in diving deeper into GraphQL, I've created a video course called Building Better APIs with GraphQL.

Features

  • Support for queries, mutations, and subscriptions
  • Input type support
  • Variable support

Installation

npm install graph.ql

Example

var Schema = require('graph.ql')

// an object of promises that fetch actual data
var loaders = require('./loaders')

// create the schema
var schema = Schema(`
  scalar Date

  type Person {
    name: String
    films: [Film]
  }

  type Film {
    title: String,
    producers(): [String]
    characters(limit: Int): [Person]
    release_date: Date
  }

  type Query {
    film(id: Int): Film
    person(id: Int): Person
  }
`, {
  Date: {
    serialize(date) {
      return new Date(date)
    }
  },
  Person: {
    films(person) {
      return loaders.film.loadMany(person.films)
    }
  },
  Film: {
    producers(film) {
      return film.producer.split(',')
    },
    characters(film, args) {
      var characters = args.limit
        ? film.characters.slice(0, args.limit)
        : film.characters

      return loaders.person.loadMany(characters)
    }
  },
  Query: {
    film(query, args) {
      return loaders.film.load(args.id)
    },
    person(query, args) {
      return loaders.person.load(args.id)
    }
  },
})

// use the schema
schema(`
  query fetch_film($id: Int) {
    film(id: $id) {
      title
      producers
      release_date
      characters {
        name
        films {
          title
        }
      }
    }
  }
`, {
  id: 1
}).then(res => console.log(res.data))

graphql-js vs graph.ql

Say we want to create a GraphQL schema that looks like this:

type Film {
  title: String
}

type Query {
  film(id: Int): Film
}

With the official graphql-js library, it would look like this:

var graphql = require('graphql')

var Film = new graphql.GraphQLObjectType({
  name: 'Film',
  fields: {
    title: {
      type: graphql.GraphQLString
    }
  }
})

var schema = new graphql.GraphQLSchema({
  query: new graphql.GraphQLObjectType({
    name: 'Query'
    fields: {
      film: {
        type: Film,
        args: {
          id: {
            description: 'Fetch the film by id',
            type: graphql.GraphQLInt
          }
        },
        resolve: (root, args) => load_film(args.id)
      }
    }
  })
})

With graph.ql, we just need to do this:

var schema = Schema(`
  type Film {
    title: String
  }

  type Query {
    # Fetch the film by id
    film(id: Int): Film
  }
`, {
  Query: {
    film(root, args) {
      return load_film(args.id)
    }
  }
})

FAQ

How do I add descriptions?

  • You can add descriptions by placing a comment directly above the field or type. The following shows a comment on a type as well as a comment on a field.
# Query methods
type Query {
  # Fetch the film by id
  film (id: Int): Film
}

Credits

Thanks to ForbesLindesay for his initial work on graphql-schema-gen which laid the groundwork for this module.

Thanks to the GraphQL team for an incredible spec as well as their kitchen sink documents to quickly test against the entire spec.

Run Tests

npm install

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap