17

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

9
  • input setInput { email: String! } getAllUsers(input: setInput): [User] Commented Oct 31, 2019 at 4:28
  • Please tell me what you want? are you want user object with condition like email address or other value Commented Oct 31, 2019 at 4:31
  • @MaheshBhatnagar I am expecting the query to return an array of the user emails. No need for an input as I'm trying to return all of them so I'm not setting any conditions Commented Oct 31, 2019 at 4:32
  • Please write that query query getAllUsers{ getAllUsers{ User[email] } } Commented Oct 31, 2019 at 4:34
  • that wouldnt work, can't include [] in queries. Now it's giving me syntax errors Commented Oct 31, 2019 at 4:37

1 Answer 1

17

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
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.