TypeError:字符串不能代表值:graphql查询不起作用

问题描述 投票:0回答:1

我正在尝试运行graphql查询,但始终显示“ TypeError:字符串不能代表值:”错误。

我的查询模式:

    type User {
        active: Boolean!
        email: String!
        fullname: String!
        description: String!
        tags: [String!]!
    }

    type Query {
        getAllUsers: [User]!
    }

我的解析器:

Query: {
        getAllUsers: (_, __, { dataSources }) => {
            return dataSources.userAPI.getAllUsers();
        }
    }

userAPI:

    getAllUsers() {
        const params = {
            TableName: 'Users',
            Select: 'ALL_ATTRIBUTES'
        };

        return new Promise((resolve, reject) => {
            dynamodb.scan(params, function(err, data) {
                if (err) {
                    console.log('Error: ', err);
                    reject(err);
                } else {
                    console.log('Success');
                    resolve(data.Items);
                }
            });
        });
    }

查询:

query getAllUsers{
  getAllUsers{
    email
  }
}

由于我的电子邮件是字符串,所以我得到的错误是“字符串不能代表值”。

node.js graphql apollo
1个回答
0
投票

解析器内部返回的内容应与架构指定的形状匹配。如果您的用户架构为

type User {
  active: Boolean!
  email: String!
  fullname: String!
  description: String!
  tags: [String!]!
}

然后您返回的用户数组应如下所示:

[{
  active: true,
  email: '[email protected]',
  fullname: 'Kaisin Li',
  description: 'Test',
  tags: ['SOME_TAG']
}]

您实际返回的数据的格式有很大不同:

[{
  active: {
    BOOL: true
  },
  description: {
    S: 'Test'
  },
  fullname: {
    S: 'Kaisin Li'
  },
  email: {
    S: '[email protected]'
  },
}]

您需要任一个映射从扫描操作获得的阵列,并将结果转换为正确的形状,或者为每个单独的字段编写一个解析器。例如:

const resolvers = {
  User: {
    active: (user) => user.active.BOOL,
    description: (user) => user.description.S,
    // and so on
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.