在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):matthewmueller/graph.ql开源软件地址(OpenSource Url):https://github.com/matthewmueller/graph.ql开源编程语言(OpenSource Language):JavaScript 99.8%开源软件介绍(OpenSource Introduction):graph.qlFaster and simpler technique for creating and querying GraphQL schemas. Video CourseIf you're interested in diving deeper into GraphQL, I've created a video course called Building Better APIs with GraphQL. Features
Installation
Examplevar 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.qlSay we want to create a GraphQL schema that looks like this:
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 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)
}
}
}) FAQHow do I add descriptions?
# Query methods
type Query {
# Fetch the film by id
film (id: Int): Film
} CreditsThanks 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
LicenseMIT |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论