I am trying to run a graphql Query but it keeps giving me the "TypeError: String cannot represent value:" error.
The schema for my query:
type User {
active: Boolean!
email: String!
fullname: String!
description: String!
tags: [String!]!
}
type Query {
getAllUsers: [User]!
}
My resolver:
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);
}
});
});
}
The query:
query getAllUsers{
getAllUsers{
email
}
}
Since my email is a string, the error I'm getting is "String cannot represent value".
What's returned inside your resolver should match the shape specified by your schema. If your User schema is
type User {
active: Boolean!
email: String!
fullname: String!
description: String!
tags: [String!]!
}
then the array of Users you return should look like this:
[{
active: true,
email: '[email protected]',
fullname: 'Kaisin Li',
description: 'Test',
tags: ['SOME_TAG']
}]
The data you're actually returning is shaped much differently:
[{
active: {
BOOL: true
},
description: {
S: 'Test'
},
fullname: {
S: 'Kaisin Li'
},
email: {
S: '[email protected]'
},
}]
You need to either map over the array you're getting from the scan operation and transform the result into the correct shape, or write a resolver for each individual field. For example:
const resolvers = {
User: {
active: (user) => user.active.BOOL,
description: (user) => user.description.S,
// and so on
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With