0

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: () => 'John.Doe@gmail.com'
    },
    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: 'John.Doe@gmail.com',
        socialMedia: {
            facebook: 'John Doe Facebook',
            instagram: 'John Doe Instagram',
            twitter: 'John Doe Twitter'
        }
    }),

Whats the hard-and-fast rule for this nonsense??

Graeme Ford
  • 166
  • 1
  • 2
  • 16
  • Passing in resolvers through root is effectively a hack and an anti-pattern. If you want to use SDL to build your schema, do not use `buildSchema`; use `makeExecutableSchema` from `graphql-tools`. Please see [this answer](https://stackoverflow.com/questions/53984094/notable-differences-between-buildschema-and-graphqlschema/53987189#53987189) for more details. – Daniel Rearden Jan 18 '19 at 14:08

1 Answers1

1

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: () => 'John.Doe@gmail.com',
        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'
    }

 };
Carlos Rufo
  • 426
  • 4
  • 6
  • YOU SIR ARE A LEGEND!!! You have saved my hairline today!!! Can you by chance explain what I was doing wrong and why that worked? – Graeme Ford Jan 18 '19 at 11:44
  • Ok, soooooo update. The user table is working but now the social media one isn't. The trick seems to be I have to use `user` not `User` which I don't really understand? Neither `SOCIALMEDIA` nor `socialMedia` work for the social media data though so I dont really know whats going on :( – Graeme Ford Jan 18 '19 at 11:55
  • In trivial examples, resolvers ~= Query fields, I've updated the example, in order to DRY over socialMedia, check https://graphql.org/learn/execution/#trivial-resolvers – Carlos Rufo Jan 18 '19 at 18:57