【问题标题】:Is there a more elegant way instead of writing lots of queries?有没有更优雅的方式来代替编写大量查询?
【发布时间】:2019-12-25 00:22:42
【问题描述】:

我正在使用 GraphQL、Apollo Express 和 MongoDB 以及 Mongoose 构建一个小型博客。

目前,文章是通过其 ID 获取的,访问者可以在此处浏览 ID 为“123”的文章:example.com/articles/123

相反,我想使用 slug,因此访问者可以访问 example.com/articles/same-article-as-above

到目前为止我的解析器:

import { gql } from 'apollo-server-express';

export default gql`
  extend type Query {
    articles: [Article!]
    article(id: ID!): Article
  }

  type Article {
    id: ID!
    slug: String!
    title: String!
    desription: String!
    text: String!
  }
`;

我可以添加另一个查询:

    articleBySlug(slug: String!): Article

这将完全正常。但是,这对我来说看起来不是很优雅,我觉得我缺少一些基本的理解。每次我尝试按标题、文本、描述或其他内容获取文章时,我真的必须向解析器添加新查询吗?我最终会得到很多查询,例如“articleByTitle”、“articleByDate”等。有人可以给我一个提示、一个例子或一些最佳实践(或者只是确认我确实必须添加越来越多的查询☺)?

【问题讨论】:

    标签: graphql apollo-server


    【解决方案1】:

    执行此操作的常用方法是将所有输入添加到同一查询中,并使其成为可选:

    export default gql`
      extend type Query {
        articles: [Article!]
        article(id: ID, slug: String, date: String, search: String): Article
      }
    
      type Article {
        id: ID!
        slug: String!
        title: String!
        description: String!
        text: String!
      }
    `;
    

    然后,在解析器中检查是否提供了 idslugdate 中的一个,如果没有则返回错误。

    另一种选择是使用类似于 Gmail 使用的搜索字符串(例如 id:x before:2012-12-12),然后在解析器中进行解析。

    export default gql`
      extend type Query {
        articles: [Article!]
        article(search: String): Article
      }
    
      type Article {
        id: ID!
        slug: String!
        title: String!
        description: String!
        text: String!
      }
    `;
    

    第三种选择是设置一个单独的搜索查询,它可以返回多种类型:

    export default gql`
      extend type Query {
        articles: [Article!]
        search(query: String!, type: SearchType): SearchResult 
      }
    
      union SearchResult = Article | User
    
      enum SearchType {
        ARTICLE
        USER
      }
    
      type Article {
        id: ID!
        slug: String!
        title: String!
        description: String!
        text: String!
      }
    
      type User {
        id: ID!
        email: String!
        name: String!
      }
    `;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多