Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: String cannot represent value: graphql Query not working

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".

like image 946
Kaisin Li Avatar asked Oct 31 '19 04:10

Kaisin Li


1 Answers

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
  }
}
like image 75
Daniel Rearden Avatar answered Nov 06 '22 10:11

Daniel Rearden