I'm just putting this here for future reference. I took a few months off from GraphQL. When I came back I coded a new resolver and it was returning with loading === false but data === 'undefined'. The solution revealed a tweaky detail about setting up GraphQL types and queries, that probably any number of people just learning GraphQL struggle with.
In seeking the answer, I searched many SO posts and other articles dealing with loading === false but data === 'undefined', but didn't see one that described the info I needed to solve it in my case.
Here's the setup that did not work:
SCHEMA
type BraintreeClientToken{
clientToken: String
}
type query{
getBraintreeClientToken(localUserId: String!): BraintreeClientToken
}
QUERY LIBRARY
This is the code that had the error
const GETBRAINTREECLIENTTOKEN_QUERY = gql`
query ($localUserId: String!) {
getBraintreeClientToken(localUserId: $localUserId) {
BraintreeClientToken
}
}
`;
RESOLVERS
getBraintreeClientToken: (parent, args, context) => {
const userid = context.userId;
let BraintreeClientToken = {
clientToken: '',
}
return Promise.resolve()
.then(() => {
//CODE HERE THAT CORRECTLY OBTAINS THE CLIENTTOKEN FROM THE BRAINTREE API -- NOT RELEVANT TO THIS POST
return clientToken;
})
.then((clientToken) => {
BraintreeClientToken.__typename = 'BraintreeClientToken';
BraintreeClientToken.clientToken = clientToken.clientToken;
return BraintreeClientToken;
})
.then((BraintreeClientToken) => {
return BraintreeClientToken;
})
.catch((err) => {
console.log(err);
});
}
Have a look at the query library code. That looked reasonable to me at first. After all, the resolver named getBraintreeClientToken is returning an object named BraintreeClientToken, and it has a typename BraintreeClientToken. Shouldn't that be what needs to be in the gql query string?
[See answer below]