В последнее время очень много хайпа вокруг GraphQL и вас наверное интересует подходит ли он для вашего проекта. В своём докладе я хочу раскрыть суть этой технологии рассказав какие проблемы она решает и как она работает в деталях. Я покажу как писать запросы, посылать их и как они исполняются на сервере. Также, я расскажу про проблемы GraphQL и о том насколько эта технология готова к использованию в продакшене
29. https://github.com/facebook/graphq
GraphQL
Working Draft – October 2016
Introduction
This is a Draft RFC Specification for GraphQL, a query language created by
Facebook in 2012 for describing the capabilities and requirements of data models
for client‐server applications. The development of this standard started in 2015.
GraphQL is a new and evolving language and is not complete. Significant
enhancement will continue in future editions of this specification.
44. 1 import * as sqlite from 'sqlite';
2 import * as express from 'express';
3 import * as graphqlHTTP from 'express-graphql';
4 import { buildSchema } from 'graphql';
Dependencies
45. 78 const db = await sqlite.open('./sw.sqlite3', {Promise});
79 const app = express();
80 app.use('/graphql', graphqlHTTP({
81 schema: buildSchema(SW_SDL),
82 rootValue: new Query(),
83 context: { db },
84 }));
85 app.listen(8888);
GraphQL Server
46. class Query {
person(args, context) {
// ...
}
}
class Person {
homeworld(args, context) {
// ...
}
}
class Planet {
residents(args, context) {
// ...
}
}
Resolvers
type Query {
person(personID: ID!): Person
}
type Person {
name: String
# ...
homeworld: Planet
}
type Planet {
name: String
residents: [Person!]
}
Schema
47. Root value
28 class Query {
29 async person(args, context) {
30 const row = await context.db.get(
31 'SELECT * FROM person WHERE id = ?',
32 args.personID,
33 );
34 if (!row) {
35 throw new Error(`Unknown personID`);
36 }
37 return new Person(row);
38 }
39 }
48. class Query {
person(args, context) {
// ...
}
}
class Person {
homeworld(args, context) {
// ...
}
}
class Planet {
residents(args, context) {
// ...
}
}
SELECT * FROM person
WHERE id = $arg.personID
SELECT * FROM planets
WHERE id = $this.planetID
SELECT * FROM person
WHERE homeworld = $this.ID
50. ● Work with any data source (SQL, NoSQL, CMS, ...)
● Can join data from multiple sources
● Can be executed in parallel
● You can write them in any languages (if it has library)
GraphQL Resolvers
51.
52. type Person {
name: String
# ...
numberOfVotes: String
averageRating: Float
}
type Mutation {
addStars(personID: ID!, numberOfStars: Int!): Person
}
60. Not covered today :(
● Type system features (Interfaces, Unions, …)
● Advance query features (Aliases, Fragments)
● Introspection (Reflection)
● Versioning - you don’t need it
● Subscriptions - Real time notifications (currently
only WebSockets)
https://graphql.org