【问题标题】:Graphql resolve mapping with makeExecutableSchemaGraphql 使用 makeExecutableSchema 解析映射
【发布时间】:2017-07-18 15:33:03
【问题描述】:

我在尝试解析 GraphQL 类型中的字段时遇到问题。 所以,在这里我试图解决patientQuery的find属性

const gqlSchema = makeExecutableSchema({
    typeDefs: `
  type patientQuery {
    find: [String]
    findOne: String
  }
  type Query {
    patient: patientQuery
  }
  type Mutation {
    addPost(name: String, title: String, content: String): patientQuery
  }
  schema {
    query: Query
    mutation: Mutation
  }
  `,
    resolvers: {
      patientQuery: {
        find(root, params, context, ast) {
          console.log('testing');
          return ['title'];
        }
      }
    }
  });

但是当我做这样的查询时

{
  patient {
    find
  }
}

我总是得到 null

{
  "data": {
    "patient": null
  }
}

那么,解析patientQuery 类型中的字段的正确方法是什么?

【问题讨论】:

  • 乍一看,我怀疑这与您正在返回一个字符串但该字段应返回一个字符串数组的事实有关。尝试返回return ['title']
  • 更改了返回数组的解析函数,但我仍然得到 null

标签: graphql graphql-js apollostack apollo-server


【解决方案1】:

所以,我发现了问题所在。 Query 类型必须解析患者字段,以便它不会返回 null。

所以新的解析器对象将如下所示,以解决问题

 resolvers: {
  Query: {
    patient() {
      return true;
    }
  },
  patientQuery: {
    find(root, params, context, ast) {
      console.log('testing');
      return [ 'title' ];
    }
  }
}

旧的:-

resolvers: {
  patientQuery: {
    find(root, params, context, ast) {
      console.log('testing');
      return ['title'];
    }
  }
}

【讨论】:

    猜你喜欢
    • 2017-07-02
    • 2021-03-24
    • 1970-01-01
    • 2019-07-23
    • 2021-11-18
    • 2019-05-21
    • 2020-04-05
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多