DEV Community

Discussion on: How to integrate GraphQL with SailsJS application

Collapse
 
vrajasekhar1 profile image
vrajasekhar1

Hi, when I try to use the following query, I get an error: "Cannot return null for non-nullable field Book.title". How to resolve this?

query ($id: Int!) {
    getAuthor(id: $id) {
        ... on Author {
          name     
          books {
            title
          }
        }
        ... on ErrorResponse {
            errors {
                code
                message
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vrajasekhar1 profile image
vrajasekhar1 • Edited

Issue is resolved now. I figured out a few minor javascript issues with using typeof on Array and also returning an array using [result]. There are different ways to achieve these. Also, _getbook, _getauthor need to be called with await and corresponding arrow functions need to be async. With these changes, all issues resolved and its working fine now. Thanks for this wonderful write up. This helped me to learn GraphQL with Sails quickly.

Collapse
 
aflatoon2874 profile image
RSA

Thanks Raj for pointing out the missing await keywords in the book and author resolvers. Have fixed the github repo as well as this article. I am glad the article helped you. :)

Thread Thread
 
vrajasekhar1 profile image
vrajasekhar1

Also, what about "typeof result"? This never returns Array. So, what's the significance of using typeof result? Instead, should we use Array.isArray(result) or "result instanceof Array"? Or am I missing something? Also, "return [ result ]" does not work for me. Is there any other simpler alternative to handle Array conversion in such cases? Thanks again.

Thread Thread
 
aflatoon2874 profile image
RSA

Yes you are right, you need to use either forms to check for array. typeof does not return array that is why the check fails.

Thread Thread
 
vrajasekhar1 profile image
vrajasekhar1

Thank you. One request, would you be able to share similar write ups on the following topics?
1) How to handle subscriptions with GraphQL?
2) How to handle file uploads using GraphQL, specifically to one of GCP or AWS?
That will be really helpful. Thanks in advance.

Thread Thread
 
aflatoon2874 profile image
RSA

You don't need graphql for both use cases. Stick to sails built-in support of sockets for pub-sub use cases and skipper package for file uploads. Skipper packages are available for AWS and GCP on npmjs.