Reputation: 196
So the GraphQL site has S#*t documentation as far as I can tell on nested queries like user spesifics and its driving me nuts (I seriously want to pull me hair out right now). Can someone please explain to me how revolvers work and why what I'm doing isn't working?
PROBLEM: I have created the following index.js file for my GraphQL API:
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');
// Define Schemas
var schema = buildSchema(`
type Query {
user: User
}
type User {
firstName: String
lastName: String
email: String
socialMedia: SOCIALMEDIA
}
type SOCIALMEDIA {
facebook: String
instagram: String
twitter: String
}
`);
// Define resolver functions
var root = {
User: {
firstName: () => 'John',
lastName: () => 'Doe',
email: () => '[email protected]'
},
SOCIALMEDIA: {
facebook: () => 'John Doe Facebook',
instagram: () => 'John Doe Instagram',
twitter: () => 'John Doe Twitter'
}
};
var app = express();
app.use('/', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(8080, () => console.log('Now browse to localhost:8080'));
And despite my best efforts, when I submit the following query:
{
user {
firstName
}
}
I get the following answer:
{
"data": {
"user": null
}
}
Why is it null
?!?! I haven't even tried to get fancy with user(id: $ID): [User]
iterations or anything resembling passing in arguments or functions even and its already giving me stick!
What am I missing about how nested fields work?
EDIT: wow... ok got it working?
user: () => ({
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
socialMedia: {
facebook: 'John Doe Facebook',
instagram: 'John Doe Instagram',
twitter: 'John Doe Twitter'
}
}),
Whats the hard-and-fast rule for this nonsense??
Upvotes: 0
Views: 652
Reputation: 446
Try this,
// Define Schemas
var schema = buildSchema(`
type Query {
getUser: User
getSocialMedia: SOCIALMEDIA
}
type User {
firstName: String
lastName: String
email: String
socialMedia: SOCIALMEDIA
}
type SOCIALMEDIA {
facebook: String
instagram: String
twitter: String
}
`);
// Define resolver functions
var root = {
getUser: {
firstName: () => 'John',
lastName: () => 'Doe',
email: () => '[email protected]',
socialMedia: {
facebook: () => 'John Doe Facebook',
instagram: () => 'John Doe Instagram',
twitter: () => 'John Doe Twitter'
}
},
getSocialMedia: {
facebook: () => 'John Doe Facebook',
instagram: () => 'John Doe Instagram',
twitter: () => 'John Doe Twitter'
}
};
Upvotes: 1