开发者问题收集

Graphql 说未定义类型的验证错误

2020-05-13
13955

因此,我在 React 应用上运行 gql 查询并传递一些默认变量。但是,当查询运行时,我收到此错误:

Error: GraphQL error: Validation error of type FieldUndefined: Field 'firstname' in type 'HealthcareWorkersPage' is undefined @ 'healthcareWorkers/firstname'

这是 .js 文件中的查询:

export const GET_PROFILES = gql`query healthcareWorkers($pageNo: Int = 0, $pageSize:Int =  6) {
        healthcareWorkers(pageNo: $pageNo, pageSize: $pageSize) {
            firstname
        }
    }`;

之前的上述查询是:

export const GET_PROFILES = gql`query {
healthcareWorkers($pageNo: Int = 0, $pageSize:Int =  6) {
        healthcareWorkers(pageNo: $pageNo, pageSize: $pageSize) {
            firstname
        }
    }
    }`;

请注意查询关键字后的 { 。但在这种情况下,我收到一条错误消息,提示 语法错误:预期名称,找到 $

我在操场上使用此查询,并且没有任何问题:

    query{
  healthcareWorkers(pageNo: 1, pageSize:6) {
    healthcareWorkers {
      firstname
    }
  }
}

healthcareWorkers 架构: Schema

1个回答

firstname 字段位于 healthcareWorkers 类型内。尝试此查询:

export const GET_PROFILES = gql`
  query healthcareWorkers($pageNo: Int = 0, $pageSize: Int = 6) {
    healthcareWorkers(pageNo: $pageNo, pageSize: $pageSize) {
      healthcareWorkers {
        firstname
      }
    }
  }
`;
user9408899
2020-05-13