graphql queries
query 请求
// 请求所有的company 及每个company 里面所有的user 和 product_category,// 每个product_category 关联的the_attribute 也会被返回query{companies{company_nameusers{username}product_categories{namethe_attributes{idname}}}}
加过滤条件
//按照ID 过滤query{companies(where:{id:"5f3aac26702c6e2efdb8c2b1"}){idcompany_nameusers{username}}}// 加返回条数query{companies(limit:2){idcompany_name}}// Fetch 1 user and 5 most recent todos for each userquery {users (limit: 1) {idnametodos(order_by: {created_at: desc}, limit: 5) {idtitle}}}//Fetch users (with limit 1), and their todos (ordered by descending creation time, and limited to 5)
变量类型
GraphQL comes with a set of default scalar types out of the box:
Int: A signed 32‐bit integer.Float: A signed double-precision floating-point value.String: A UTF‐8 character sequence.Boolean: true or false.ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.eg:query supplier($id: ID!) {}后面加感叹号表示值不能为 null! after the type name. This means that our server always expects to return a non-null value for this field
Passing arguments to your queries dynamically
query ($limit: Int!) {todos(limit: $limit) {idtitle}}In addition to the query above, we send a variables object:{"limit": 10}// 排序 asc(正序排列) 和 desc(倒序排列)query cus_teacher($sort: String) {teachers(sort: $sort){idnameage}}{"sort": "name:desc"}//多个过滤条件 start 和 sortquery cus_teacher($start: Int,$sort: String) {teachers(start: $start,sort: $sort){idnameage}}{"start": 1,"sort": "id:asc"}// 前端运用实例export async function fetchAPI(query:string, { variables } = {}) {return request(`${API_URL}/graphql`, {method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify({query,variables,}),})}export async function getStoreList() {let condition = {}return fetchAPI(`query HotStores($where: JSON) {hotStores(where:$where) {idstaffLikestaffDislikestore_tags{idname}names(where:{type:"initialValue"}){idtypevalue}}}`,{ variables: { "where": condition} })}
给某个字段加别名 alias
// 根据 不同的条件查询出不同的 keyword_ranks 数组,//分别将对应的字段命名为keyword_ranks_creative,keyword_ranks_p4pquery keywords($where: JSON, $aliplatform_num: Int!) {keywords(where:$where, limit: 1000) {idnamekeyword_ranks_creative:keyword_ranks(where:{type:"creative"},sort:"time:desc",limit: $aliplatform_num){idranktypetimedescriptionaliplatform{idname}}keyword_ranks_p4p:keyword_ranks(where:{type:"p4p"},sort:"time:desc",limit: $aliplatform_num){idranktypedescriptionaliplatform{idname}}sem_search_volumn}}