日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

GraphQL入门2

發(fā)布時(shí)間:2024/4/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GraphQL入门2 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

將服務(wù)器端的代碼升級(jí)了一下:

var GraphQLSchema = require('graphql').GraphQLSchema;

var GraphQLObjectType = require('graphql').GraphQLObjectType;

var GraphQLString = require('graphql').GraphQLString;

var GraphQLList = require('graphql').GraphQLList;

var fetch = require('node-fetch');

require("babel-polyfill");

?

var BASE_URL = 'http://localhost:3000';

?

var persons = [

??? { id: "1", first_name: "Jack", last_name: "Zhang" , department: "Depart1", friends: [1] },

??? { id: "2", first_name: "Tom", last_name: "Wang" , department: "Depart2", friends: [1, 2] }

];

?

function getPersonByUrl(args, relativeURL) {

??? var person = persons.find(function (item) {

??????? if (args.id) {

??????????? return item.id == args.id;

??????? }

?

??????? if (args.department) {

??????????? return item.department == args.department;

??????? }

???????

??? });

?

??? return person;

?

??? //fetch('${BASE_URL}${relativeURL}')

??? //??? .then(function (res) { return res.json() })

??? //??? .then(function (json) { return json.person })

}

?

?

function getFriendByPersonId(friendID) {

?

??? var person = persons.find(function (item) {

??????? return item.id == friendID;

??? });

???

??? return person;

?

??? //fetch('${BASE_URL}${relativeURL}')

??? //??? .then(function (res) { return res.json() })

??? //??? .then(function (json) { return json.person })

}

?

?

var PersonType = new GraphQLObjectType( {

??? name: 'Person',

??? description: '...',

??? fields: ()=>({

??????? id: {

??????????? type: GraphQLString,

??????????? resolve : function (person) {

??????????????? return person.first_name;

??????????? }

??????? },

??????? firstName: {

??????????? type: GraphQLString,

??????????? resolve : function (person) {

??????????????? return person.first_name;

??????????? }

??????? },

??????? lastName: {

??????????? type: GraphQLString,

??????????? resolve : function (person) {

???????????? ???return person.last_name;

??????????? }

??????? },

??????? department: {

??????????? type: GraphQLString,

??????????? resolve : function (person) {

??????????????? return person.department;

??????????? }

??????? },

??????? //email: { type: GraphQLString },

??????? //userName: { type: GraphQLString },

??????? //id: { type: GraphQLString },

??????? friends: {

??????????? type: new GraphQLList(PersonType),

??????????? resolve: function (person) {

??????????????? return person.friends.map(getFriendByPersonId);

??????????? }

??????? }

??? })

});

?

var QueryType = new GraphQLObjectType({

??? name: 'Query',

??? desription: '...',

??? fields: {

??????? person: {

??????????? type: PersonType,

??????????? args: {

??????????????? id: { type: GraphQLString },

???????? ???????department: { type: GraphQLString }

??????????? },

??????????? resolve: function (obj, args, context, info) { return getPersonByUrl(args, null); }

??????? }

??? }

???

});

?

var GraphQLSchemaObj = new GraphQLSchema({

??? query: QueryType

});

?

?

module.exports = GraphQLSchemaObj;

?

主要有以下幾處更改:

  • 將模擬的數(shù)據(jù)源persons單獨(dú)提出來(lái)成為一個(gè)全局變量.
  • 數(shù)據(jù)源中增加了department等幾個(gè)屬性.
  • getPersonByUrl函數(shù)支持id和department參數(shù).
  • 增加了getFriendByPersonId函數(shù)用來(lái)解析friends屬性.
  • PersonType的fields屬性使用了()=>來(lái)解決friends屬性中使用本類型時(shí)本類型尚未初始化的問(wèn)題.
  • ?

    下面是客戶端的測(cè)試代碼:

    app.js

    console.log('Hello world');

    ?

    Arguments

    //var Test1 = require('./Test1');

    //Test1.Execute();

    ?

    Alias

    //var Test2 = require('./Test2');

    //Test2.Execute();

    ?

    Alias with sub-selection

    //var Test3 = require('./Test3');

    //Test3.Execute();

    ?

    Fragments

    //var Test4 = require('./Test4');

    //Test4.Execute();

    ?

    //Variblies

    //var Test5 = require('./Test5');

    //Test5.Execute();

    ?

    //Directives

    //var Test6 = require('./Test6');

    //Test6.Execute();

    ?

    具體的測(cè)試類:

    Alias:

    //Test2: Aliases?

    ?

    var gRequest = require('graphql-request').request;

    ?

    exports.Execute = function () {

    ?

    ??? const query = '{'

    ??????????????? + '? Depart1Person: person(department: "Depart1") {'

    ??????????????? + '??? firstName,'

    ??????????????? + '??? lastName,'

    ???????? ???????+ '??? department'

    ??????????????? + '? }'

    ??????????????? + '? Depart2Person: person(department: "Depart2") {'

    ??????????????? + '??? firstName,'

    ??????????????? + '??? lastName,'

    ??????????????? + '??? department'

    ??????????????? + '? }'

    ??????????????? + '}';

    ???????

    ??? gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) });

    ?

    };

    ?

    ???????????????

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    運(yùn)行結(jié)果如下:

    { Depart1Person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1' },

    ? Depart2Person: { firstName: 'Tom', lastName: 'Wang', department: 'Depart2' } }

    ?

    Aliases -- sub selection

    //Test3: Aliases -- sub selection

    ?

    var gRequest = require('graphql-request').request;

    var util = require('util');

    ?

    exports.Execute = function () {

    ?

    ??? const query = '{'

    ??????????????? + '? Depart1Person: person(department: "Depart1") {'

    ??????????????? + '??? firstName,'

    ??????????????? + '??? lastName,'

    ??????????????? + '??? department,'

    ??????????????? + '??? friends'

    ??????????????? + '??? {'

    ?????????????? ?+ '???? firstName,'

    ??????????????? + '???? lastName'

    ??????????????? + '??? }'

    ??????????????? + '? }'

    ??????????????? + '? Depart2Person: person(department: "Depart2") {'

    ??????????????? + '??? firstName,'

    ??????????????? + '??? lastName,'

    ?????? ?????????+ '??? department,'

    ??????????????? + '??? friends'

    ??????????????? + '??? {'

    ??????????????? + '???? firstName,'

    ??????????????? + '???? lastName'

    ??????????????? + '??? }'???

    ??????????????? + '? }'

    ??????????????? + '}';

    ???????

    ??? gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) {

    ??????? console.log(util.inspect(data, { showHidden: false, depth: null }))

    ??? });

    ?

    };

    ?

    運(yùn)行結(jié)果如下:

    { Depart1Person:

    ?? { firstName: 'Jack',

    ???? lastName: 'Zhang',

    ???? department: 'Depart1',

    ???? friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] },

    ? Depart2Person:

    ?? { firstName: 'Tom',

    ???? lastName: 'Wang',

    ???? department: 'Depart2',

    ???? friends:

    ????? [ { firstName: 'Jack', lastName: 'Zhang' },

    ??????? { firstName: 'Tom', lastName: 'Wang' } ] } }

    ?

    Fragements:

    //Test4: Fragements

    ?

    var gRequest = require('graphql-request').request;

    var util = require('util');

    ?

    exports.Execute = function () {

    ?

    ??? var query = '{'

    ??????????????? + '? Depart1Person: person(department: "Depart1") {'

    ??????????????? + '??? ...personFields'

    ??????????????? + '? }'

    ??????????????? + '? Depart2Person: person(department: "Depart2") {'

    ??????????????? + '??? ...personFields'

    ??????????????? + '? }'

    ??????????????? + '}'

    ??????????????? + ''

    ??????????????? + 'fragment personFields on Person {'

    ??????????????? + '? firstName,'

    ??????????????? + '? lastName,'

    ??????????????? + '? department,'

    ??????????????? + '? friends{'

    ??????????????? + '??? firstName,'

    ??????????????? + '??? lastName'

    ??????????????? + '? }'

    ??????????????? + '}';

    ???????

    ??? //gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) });

    ??? gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) {

    ??????? console.log(util.inspect(data, { showHidden: false, depth: null }))

    ??? });

    ?

    };

    ?

    運(yùn)行結(jié)果如下:

    { Depart1Person:

    ?? { firstName: 'Jack',

    ???? lastName: 'Zhang',

    ???? department: 'Depart1',

    ???? friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] },

    ? Depart2Person:

    ?? { firstName: 'Tom',

    ???? lastName: 'Wang',

    ???? department: 'Depart2',

    ???? friends:

    ????? [ { firstName: 'Jack', lastName: 'Zhang' },

    ??????? { firstName: 'Tom', lastName: 'Wang' } ] } }



    Varibles:

    //Test5: Variables

    ?

    var gRequest = require('graphql-request').request;

    var util = require('util');

    ?

    exports.Execute = function () {

    ?

    ??? var query = 'query PersonWithDept($dept: String) {'

    ??????????? + '? person(department: $dept) {'

    ??????????? + '??? ...personFields'

    ??????????? + '? }'

    ??????????? + '}'

    ??????????? + ''

    ??????????? + 'fragment personFields on Person {'

    ??????????? + '? firstName,'

    ??????????? + '? lastName,'

    ??????????? + ' ?department,'

    ??????????? + '? friends{'

    ??????????? + '??? firstName,'

    ??????????? + '??? lastName'

    ??????????? + '? }'

    ??????????? + '}';

    ???

    ??????????? var varibles =

    ??????????? {

    ??????????????? "dept": "Depart1"

    ??????????? };

    ???????

    ??? //gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) });

    ??? gRequest('http://localhost:1337/graphql/graphql', query, varibles).then(function (data) {

    ??????? console.log(util.inspect(data, { showHidden: false, depth: null }))

    ??? });

    ?

    };

    ?

    運(yùn)行結(jié)果如下:

    { person:

    ?? { firstName: 'Jack',

    ???? lastName: 'Zhang',

    ???? department: 'Depart1',

    ???? friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] } }???

    ?

    Directives:

    //Test6: Directives?

    ?

    var gRequest = require('graphql-request').request;

    var util = require('util');

    ?

    exports.Execute = function () {

    ?

    ??? var query = 'query PersonWithDept($dept: String, $withFriends: Boolean!) {'

    ??????????????? + '? person(department: $dept) {'

    ??????????????? + '??? ...personFields'

    ??????????????? + '? }'

    ??????????????? + '}'

    ??????????????? + ''

    ??????????????? + 'fragment personFields on Person {'

    ??????????????? + '? firstName,'

    ??????????????? + '? lastName,'

    ??????????????? + '? department,'

    ??????????????? + '? friends @include(if: $withFriends){'

    ??????????????? + '??? firstName,'

    ??????????????? + '??? lastName'

    ??????????????? + '? }'

    ??????????????? + '}' ;

    ???

    ??????? var varibles1 =

    ??????? {

    ??????????? "dept": "Depart1",

    ??????????? "withFriends": true

    ??????? };

    ???

    ??????? var varibles2 =

    ??????? {

    ??????????? "dept": "Depart1",

    ??????????? "withFriends": false

    ??????? };

    ???????

    ??? //gRequest('http://localhost:1337/graphql/graphql', query).then(function (data) { console.log(data) });

    ??? gRequest('http://localhost:1337/graphql/graphql', query, varibles1).then(function (data) {

    ??????? console.log(util.inspect(data, { showHidden: false, depth: null }))

    ??? });

    ?

    ??? gRequest('http://localhost:1337/graphql/graphql', query, varibles2).then(function (data) {

    ??????? console.log(util.inspect(data, { showHidden: false, depth: null }))

    ??? });

    ?

    };

    ?

    運(yùn)行結(jié)果如下:

    { person: { firstName: 'Jack', lastName: 'Zhang', department: 'Depart1' } }

    { person:

    ?? { firstName: 'Jack',

    ???? lastName: 'Zhang',

    ?? ??department: 'Depart1',

    ???? friends: [ { firstName: 'Jack', lastName: 'Zhang' } ] } }

    ?

    注意客戶端代碼中使用了,是為了打印出json的子對(duì)象,

    總結(jié)

    以上是生活随笔為你收集整理的GraphQL入门2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。