0

Schema:

const graphql = require('graphql');
const _ = require('lodash');
const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLSchema
} = graphql;

const users = [
    { id: '23', firstName: 'Bill', age: 20 },
    { id: '47', firstName: 'Samantha', age: 21 }
]

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: {
        id: { type: GraphQLString },
        firstName: { type: GraphQLString },
        age: {type: GraphQLInt },
    }
});

const RootQueryType = new GraphQLObjectType({
    name: 'RootQuery',
    fields: {
        user: {
            type: UserType,
            args: { id: {type: GraphQLString} },
            resolve(parentValue, args) {
                return _.find(users, args.id);
            }
        }
    }
});

module.exports = new GraphQLSchema({
    query: RootQueryType,
});

Express:

const express = require('express');
const expressGraphQL = require('express-graphql');
const schema = require('./schema/schema')

const app = express();

app.use('/graphql', expressGraphQL({
    schema,
    graphiql: true,
}));

app.listen(4000, () => {
    console.log('Listening');
});

Query:

{
  user(id:"23") {
    id,
    firstName,
    age
  }
}

Trying to learn GraphQL and can't for the life of me figure out what's wrong with my query/schema.

afkbowflexin
  • 4,029
  • 2
  • 37
  • 61

1 Answers1

1

You just need to change how you're calling lodash's find:

return _.find(users, { id: args.id });
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183