GraphQL增量学习05-graphql-resolvers-union-union的使用目标union使用unionunion代码graphQL-example/features.jsStep1.准备测试Staticdataconstnotices=[{id:1,content:'Thisisanotice',noticeTime:1524710641}]constreminders=[{id:1,content:'Thisisareminder',endTime:1524710641}]noticesnotificationremindersReminder2.写typeDefsconsttypeDefs=`scalarDateinterfaceMessage{content:String}typeNoticeimplementsMessage{content:StringnoticeTime:Date}typeRemindimplementsMessage{content:StringendTime:Date}"""联合类型通知和提醒"""unionMessageResult=Notice|提醒typeQuery{searchUnion(text:String!):MessageResult!}`MessageResultunionobjectsearchUnionforclientquerymethod3.写resolversconstresolvers={Query:{searchUnion:(_,{text})=>{if(text==='notice'){returnnotices[0]}else{returnreminders[0]}}},MessageResult:{__resolveType(obj,context,info){console.log(obj,context,info)if(obj.noticeTime){return'Notice'}if(obj.endTime){return'Remind'}returnnull}},Date:newGraphQLScalarType({name:'Date',description:'日期自定义标量类型',parseValue(value){returnnewDate(value)//来自客户端的值},serialize(value){//returnnewDate(value).getTime()returnnewDate(value)//发送给客户端的值},parseLiteral(ast){if(ast.kind===Kind.INT){returnparseInt(ast.value,10)//ast值总是字符串格式}returnnull}})}searchUnion搜索方法实现,直接返回一个通知或提醒__resolveType返回类型定义if(obj.noticeTime){如果包含noticeTime字段,则判断为通知4.MergeSchemaconstschema=makeExecutableSchema({typeDefs,resolvers})测试1.请求searchUnion方法#Requestqueryquerydo($text:String!){searchUnion(text:$text){...onNotice{contentnoticeTime}...onRemind{contentendTime}}}#variable变量bles{"text":"notice"}#Output{"data":{"searchInterface":{"content":"Thisisanotice","endTime":"1970-01-18T15:31:50.641Z"}}}我们可以尝试提升content字段,发现查询报错,和接口还是有区别的输入\"MessageResult\"。您的意思是要在\"Message\"、\"Notice\"或\"Remind\"上使用内联片段吗?","locations":[{"line":3,"column":5}]}]}详见04-graphql-resolvers-interfaces-接口2的使用print__resolveType(obj,context,info)parameter#obj{id:1,content:'Thisis通知',noticeTime:1524710641}#context{}#info{fieldName:'searchUnion',fieldNodes:[{kind:'Field',别名:undefined,name:[Object],arguments:[Array],directives:[],selectionSet:[Object],loc:[Object]}],returnType:MessageResult!,parentType:Query,path:{prev:undefined,key:'searchUnion'},schema:GraphQLSchema{__allowedLegacyNames:undefined,_queryType:Query,_mutationType:Mutation,_subscriptionType:null,_directives:[[Object],[Object],[Object]],astNode:undefined,_typeMap:{Query:Query,Post:Post,Int:Int,String:String,Author:Author,Country:Country,Message:Message,MessageResult:MessageResult,Notice:Notice,Date:Date,Remind:Remind,Mutation:Mutation,AuthorInput:AuthorInput,__Schema:__Schema,__Type:__Type,__TypeKind:__TypeKind,Boolean:Boolean,__Field:__Field,__InputValue:__InputValue,__EnumValue:__EnumValue,__Directive:__Directive,__DirectiveLocation:__DirectiveLocation},_implementations:{Message:[Array]},__validationErrors:[],_possibleTypeMap:{MessageResult:[对象]}},片段:{},rootValue:undefined,operation:{kind:'OperationDefinition',operation:'query',name:{kind:'Name',value:'do',loc:[Object]},variableDefinitions:[[Object]],指令:[],selectionSet:{kind:'SelectionSet',selections:[Array],loc:[Object]},loc:{start:0,end:173}},variableValues:{text:'notice'}}参考graphql-工具
