【问题标题】:GraphQL Clojure: Getting the GQL query fieldsGraphQL Clojure:获取 GQL 查询字段
【发布时间】:2020-05-29 08:46:57
【问题描述】:

我正在为我的 GraphQL Clojure 服务器使用 lacinia 库。
对于这个简单的架构:

input QueryConfig {
  startDate: String!
  endDate: String!
}


type MyData{
  x: Float!
  y: Float!
  z: Float!
}

schema {
    query: Query
}

type Query {
  myQuery(config:QueryConfig) : [MyData]
}

我发布以下查询:

{
  myQuery(config:{startDate:"2020-01-01",endDate:"2020-01-01"}){
    x
  }
}

在我的解析器代码中,我想知道询问了哪些字段(上面示例中的 x),因此我不会从我的数据库中过度获取结果并检索 x 值。

在解析器代码中,上下文包含 :graphql-query 键,其中包含整个查询。但是,解析查询文本似乎很尴尬。

(defn my-resolver
  [context args value]
  ;TODO find out what fields were asked and fetch from DB
  )

获取查询字段的正确方法是什么?

【问题讨论】:

    标签: server clojure graphql schema resolver


    【解决方案1】:

    来自docs

    也可以获取所有将被选中的字段,使用 选择序列。这是一个懒惰的、广度优先的导航所有领域 选择树。

    因此您可以使用:(executor/selections-seq context) 获取查询发送的所有字段。

    【讨论】:

      【解决方案2】:

      Lacinia 确实允许您预览嵌套选择:

      字段解析器可以“预览”将在其下方选择哪些字段 在选择树中。这是一个经常用来优化的工具 数据检索操作。

      来自docs的示例代码:

      (require
        '[com.walmartlabs.lacinia.executor :as executor])
      
      (defn resolve-hero
        [context args _]
        (if (executor/selects-field? context :character/friends)
          (fetch-hero-with-friends args)
          (fetch-hero args)))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-13
        • 2018-08-04
        • 2015-04-18
        • 2020-08-01
        • 1970-01-01
        • 2019-06-29
        • 2020-06-13
        • 2020-01-20
        相关资源
        最近更新 更多