Also, why is there a need to loop through the data...
# orm-help
i
Also, why is there a need to loop through the data and not just return customerData?
w
Coz if I console log for "customerData" before the for loop, I get the result in square brackets like so. [ { id: '1', first_name: 'Bob', last_name: 'James' }, { id: '2', first_name: 'Ann', last_name: 'Mary' } ]
t
@immanuelrosal good on your for helping out...try to stick to threads though, I also replied after you in the thread, because I didn't see you already did in the general thread.
@Waweru what kind of return are you expecting? square brackets is an array. You're getting an array of objects.. what are you trying to get that returning the customer data directly doesn't work?
The resolver can't return multiple times, so if you want multiple records you need to return an array (
customerData
) and loop through it in your app/frontend to use the individuals records
w
If I return customerData directly, then run the following on GraphiQL:
Copy code
{
  customers {
    id
    first_name
    last_name
  }
}
I get the following output:
Copy code
{
  "data": {
    "customers": {
      "id": null,
      "first_name": null,
      "last_name": null
    }
  }
}
t
but your console.log does show the correct data is inside customerData?
can you post the exact result from console.log?
better yet, can you
console.log(customerData)
and post that?
w
This is the output from console.log(customerData): [ { id: '1', first_name: 'Bob', last_name: 'James' }, { id: '2', first_name: 'Ann', last_name: 'Mary' } ]
@tsdexter So you mean that I can now loop that in the client front end, even if it shows null on the GraphiQL?
t
no
sorry, I thought you were getting the array as a result, not nulls
w
Yea. I'm getting an array on console.log, but null on GraphiQL. Sorry if I wasn't clear before.
t
Hmm. I think you need to return an object with a
customers
array (according to a comment here: https://jaketrent.com/post/return-array-graphql/)
Copy code
customerData = await axios(`${BaseURL}/customers`)
	.then(resp => resp.data);
return { customers: customerData };
missed
const
Copy code
const customerData = await axios(`${BaseURL}/customers`)
    .then(resp => resp.data);
return { customers: customerData };
w
I GOT IT!!!
The customers type is supposed to be GraphQLList and not GraphQLString, like: customers:{ type: new GraphQLList(CustomerType), async resolve (){ return await axios.get(
${BaseURL}/customers
) .then(resp => resp.data); } }
i
@tsdexter yeah sry about not creating a thread. Last time I used slack, it wasn't a feature yet. I'm only using slack now because of graphcool/prisma to contribute when anyone has issues. @Waweru glad your issue's resolved. 🙂
👌 1
w
@tsdexter Thank you very much for your help. I really appreciate it!!!😃
Thank you too @immanuelrosal 😁
t
Ah, that makes perfect sense now... I remember
GraphQLList
now from my first foray into graphql, building the actual server... haven't build one since as I've been using Graphcool which doesn't need a server... glad it's working!