当前位置: 首页 > 后端技术 > Node.js

GraphQL渐进式学习04-graphql-resolvers-interfaces-接口的使用

时间:2023-04-03 22:32:51 Node.js

目标使用接口接口代码graphQL-example/features.jsStep1.测试准备Staticdataconstnotices=[{id:1,content:'Thisisanotice',noticeTime:1524710641}]constreminders=[{id:1、content:'Thisisreminder',endTime:1524710641}]noticesnoticeremindersreminder2.写typeDefsconsttypeDefs=`scalarDateinterfaceMessage{content:String}typeNoticeimplementsMessage{content:StringnoticeTime:Date}typeRemindimplements消息{内容:字符串结束时间:日期}类型查询{searchInterface(文本:字符串!):消息!}`Message接口对象NoticeRemind对象实现接口MessagesearchInterface为客户端查询方法3.写resolversconstresolvers={Query:{searchInterface:(_,{text})=>{if(text==='notice'){returnnotices[0]}else{returnreminders[0]}}},消息:{__resolveType(obj,context,info){console.log(obj,context,info)if(obj.noticeTime){return'Notice'}if(obj.endTime){返回'Remind'}returnnull}},Date:newGraphQLScalarType({name:'Date',description:'Datecustomscalartype',parseValue(value){returnnewDate(value)//来自客户端的值},serialize(value){//returnnewDate(value).getTime()returnnewDate(value)//发送给客户端的值},parseLiteral(ast){if(ast.kind===Kind.INT){returnparseInt(ast.value,10)//astvalue始终为字符串格式}returnnull}})}searchInterface搜索方法实现,直接返回通知或提醒__resolveType返回类型定义if(obj.noticeTime){如果包含noticeTime字段判断就是通知4.MergeSchemaconstschema=makeExecutableSchema({typeDefs,resolvers})测试1.请求searchInterface方法#Requestqueryquerydo($text:String!){searchInterface(text:$text){content...onNotice{noticeTime}...onRemind{endTime}}}#variablevariables{"text":"notice"}#output{"data":{"searchInterface":{"content":"这不是ice","endTime":"1970-01-18T15:31:50.641Z"}}}让我们修改请求Query#requestqueryquerydo($text:String!){searchInterface(text:$text){...onNotice{contentnoticeTime}...onRemind{contentendTime}}}#variablevariables{"text":"notice"}#output{"data":{"searchInterface":{"content":"Thisisreminder","endTime":"1970-01-18T15:31:50.641Z"}}}可以发现返回的效果是一致的,这个和union还是有区别的,具体请移步05-graphql-resolvers-union-union使用2.打印__resolveType(obj,context,info)参数#obj{id:1,content:'Thisisanotice',noticeTime:1524710641}#context{}#info{fieldName:'searchInterface',fieldNodes:[{kind:'Field',别名:undefined,name:[Object],arguments:[Array],directives:[],selectionSet:[Object],loc:[Object]}],returnType:Message,parentType:查询,路径:{prev:undefined,key:'searchInterface'},架构:GraphQLSchema{__allowedLegacyNames:undefined,_queryType:查询,_mutationType:突变,_subscriptionType:null,_directives:[[对象],[对象],[对象]],astNode:未定义,_typeMap:{查询:查询,发布:发布,Int:Int,字符串:字符串,Author:Author,Country:Country,Message:Message,MessageResult:MessageResult,Notice:Notice,Date:Date,Remind:Remind,Mutation:Mutation,AuthorInput:AuthorInput,__Schema:__Schema,__Type:__Type,__TypeKind:__TypeKind,布尔值:布尔值,__Field:__Field,__InputValue:__InputValue,__EnumValue:__EnumValue,__Directive:__Directive,__DirectiveLocation:__DirectiveLocation},_implementations:{消息:[Array]},__validationErrors:[],_possibleTypeMap:{消息:[Object]}},片段:{},rootValue:未定义,操作:{kind:'OperationDefinition',operation:'query',name:{kind:'Name',value:'do',loc:[Object]},variableDefinitions:[[Object]],指令:[],selectionSet:{kind:'SelectionSet',selections:[Array],loc:[Object]},loc:{start:0,end:160}},variableValues:{text:'notice'}}参考graphql-tools

最新推荐
猜你喜欢