Load —-graphQL on demand
-
Background: When the data returned by the back end is given too many unneeded fields, graphQL loads the required data on demand
-
Steps:
- Define datapool root(which contains all data)
Var root = {girls: [{id: 1, name: 'goddess ', iPhone: 12345678910, weixin: 'xixixixi', height: 175, school:' Cambridge university ', wheel: [{name: 'spare tire 1' money: '240000'}, {name: 'spare tire 2' money: '260000'}]}, {id: 2, name: the goddess '2', the iphone: 12345678910, weixin: 'hahahahah', height: 168, school: 'Harvard University ', wheel: [{name:' spare tire 3 ', money: '800000 yuan'}, {name: 'Spare tire no. 4 ', money: '2 million yuan'}]}Copy the code
- Describes the data structure schema in a data pool
const { buildSchema } = require('graphql'); Var schema = buildSchema(' type Wheel {name: String, money: String} type Info {id: Int name: String iphone: Int weixin: String height: Int school: String wheel: [Wheel] } type Query { girls: [Info] }`);Copy the code
- User-defined query data query
const { graphql } = require('graphql'); Const query = '{girls {name wheel {money}}}'; // Query data const result = await graphQL (schema, query, root)Copy the code