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

GraphQL渐进式学习02-GraphQL-组件组成知识整理

时间:2023-04-03 22:35:31 Node.js

02-GraphQL-组件组成知识整理目的整理GraphQL组件的知识精华。组件组成有5个方面:查询更改queriesSchema和类型schema验证validationexecutionexecution保存自省方便以后在手册中参考。结合以上特征,写个代码,方便记忆graphQL-example/features。){author(id:1){idfirstNamelastName}}别名{author(id:1){idname:firstNamelastName}}Fragments{author(id:1){...authorFields}}fragmentauthorFieldsonAuthor{idfirstNamelastNameposts{title}}Operationname(操作名称)queryfindAuthors{authors{idfirstNamelastNameposts{title}}}Variables(变量)input#queryqueryfindAuthor($id:Int!){author(id:$id){idfirstNamelastNameposts{title}}}#variables{"id":1}输出默认变量(Defaultvariables)queryfindAuthor($id:Int=2){author(id:$id){idfirstNamelastNameposts{标题}}}指令#queryqueryfindAuthor($id:Int=1,$withPosts:Boolean!){author(id:$id){idfirstNamelastNameposts@include(if:$withPosts){title}}}#variables{"withPosts":false}@include(if:Boolean)仅当参数为真时包含此字段@skip(if:Boolean)如果参数为真则跳过此字段。Mutations#querymutationupPost($id:Int!){upvotePost(postId:$id){idtitlevotes}}#variables{"id":1}multiplefieldsinmutations#querymutationupPost($id:Int!){upVotePost(postId:$id){idtitlevotes}clearVotePost(postId:$id){idtitlevotes}}#variables{“id”:1}#output{“data”:{“upVotePost”:{“id”:1、"title":"GraphQL简介","votes":1},"clearVotePost":{"id":1,"title":"GraphQL简介","votes":0}}}多处修改顺序执行,但查询是一个并行的内联片段(InlineFragments)查询HeroForEpisode($ep:Episode!){hero(episode:$ep){name...onDroid{primaryFunction}...onHuman{height}}}元字段(Metafields){authors{__typenameidfirstNamelastName}}__typename显示类型名称其他描述内省系统的元字段Schema和typeschemaschema的定义是服务器代码对象类型和字段(ObjectTypesandFields)typeAuthor{"""序列号"""id:诠释!firstName:StringlastName:String"""Postspostedbyusers"""posts:[Post]}typedefinitionobject"""tagcommentIntStringsystemobject!cannotbeempty[...]arrayString!character字符串不是empty[String]!Arrayisnotempty默认标量类型Int有符号32位整数Float有符号双精度浮点值StringUTF-8字符序列Booleantrue或falseIDID标量类型表示一个唯一标识符,通常使用的ID类型是以与String相同的方式序列化以检索对象或作为缓存中的键;但是将其定义为ID意味着不需要可读。自定义类型标量Date详见03-GraphQL-标量-自定义类型枚举类型(EnumerationTypes)enumCountry{CNENGJPUKCA}typeAuthor{"""序号"""id:Int!firstName:StringlastName:Stringstate(state:Country=CN):String"""该作者的帖子列表"""posts:[Post]}state字段指定枚举类型,默认值为CN接口(Interfaces)interfaceMessage{content:String}typeNoticeimplementsMessage{content:StringnoticeTime:Date}typeRemindimplementsMessage{content:StringendTime:Date}详见04-graphql-resolvers-interfaces-UsingUnion接口类型(UnionTypes)unionMessageResult=Notice|Remind详情请移步05-graphql-resolvers-union-joint使用输入类型(InputTypes)#定义输入类型`AuthorInput`inputAuthorInput{firstName:StringlastName:Stringstate:String}#specifyChangevariabletypetypeMutation{addAuthor(author:AuthorInput!):Author}ValidationValidation通过使用类型系统,您可以预测查询是否有效。这允许服务器和客户端高效地在不依赖运行时检查的情况下创建无效查询时通知开发人员。验证GraphQL查询后,GraphQL服务器将执行它并返回与请求的结构相对应的结果,通常为JSON格式。query:{human(obj,args,context){returncontext.db.loadHumanByID(args.id).then(userData=>newHuman(userData))}}obj为上层对象,如果字段属于根节点查询类型通常不使用。args可以提供在GraphQL查询中传入的参数。上下文提供给所有解析器并保存重要的上下文信息,例如当前登录的用户或数据库访问对象。省内省__schema查询哪些对象可用#查询{__schema{types{namekind}}}#输入{"data":{"__schema":{"types":[{"name":"Query","kind":"OBJECT"},{"name":"Post","kind":"OBJECT"},{"name":"Int","kind":"SCALAR"},{"name":"String","kind":"SCALAR"},{"name":"Author","kind":"OBJECT"},{"name":"Country","kind":"ENUM"},{"name":"Message","kind":"INTERFACE"},{"name":"MessageResult","kind":"UNION"},{"name":"Notice","kind":"OBJECT"},{"name":"Date","kind":"SCALAR"},{"name":"Remind","kind":"OBJECT"},{"name":"突变","kind":"OBJECT"},{"name":"AuthorInput","kind":"INPUT_OBJECT"},{"name":"__Schema","kind":"OBJECT"},{"name":"__Type","kind":"OBJECT"},{"name":"__TypeKind","kind":"ENUM"},{"name":"Boolean","kind":"SCALAR"},{"name":"__Field","kind":"OBJECT"},{"name":"__InputValue","kind":"OBJECT"},{"name":"__EnumValue","kind":"OBJECT"},{"name":"__Directive","kind":"OBJECT"},{"name":"__DirectiveLocation","kind":"ENUM"}]}}__Schema,__Type,__TypeKind,__Field,__InputValue,__EnumValue,__Directive-这些带有两个下划线的类型是内省系统的一部分queryType查询对象名称#query{__schema{queryType{name}}}#output{"data":{"__schema":{"queryType":{"name":"Query"}}}}__typequeryobject#query{__type(name:"Notice"){名称种类描述字段{名称类型{名称}描述}接口{名称描述}}}#输出{“数据”:{“__type”:{“名称”:“通知”,“种类”:“对象”,“描述":"通知对象","fields":[{"name":"content","type":{"name":"String"},"description":"通知内容"},{"name":"noticeTime","type":{"name":"Date"},"description":"通知时间"}],"interfaces":[{"name":"Message","description":"消息界面"}]}}}所有查询对象#Query{__type(name:"Query"){namekindfields{namedescriptionargs{namedescriptiondefaultValue}}}}#输出{"data":{"__type":{"name":"Query","kind":"OBJECT","fields":[{"name":"posts","description":"所有文章","args":[]},{"name":"authors","description":"所有作者","args":[]},{"name":"author","description":"","args":[{"name":"id","description":"作者ID","defaultValue":null}]},{"name":"searchInterface","描述”:“”,“args”:[{“名称”:“文本”,“描述”:“”,“默认值”:空}]},{“名称”:“searchUnion”,“描述”:“","args":[{"名称":"文本","描述":"","defaultValue":null}]}]}}}参考QueriesandMutationsSchemasandTypesValidationExecutionIntrospectionapollodatasimple

最新推荐
猜你喜欢