jrcichra
jrcichra

Reputation: 345

Apollo GraphQL - resolver not being triggered

I'm new to GraphQL. I started with the Javascript Apollo library and cannot get a resolver to fire. It's hard for me to tell when resolvers should be called, and what key I should put.

Here's my schema and resolvers:

// The GraphQL schema in string form
const typeDefs = `
  type Query { user(id: ID!): User, posts: Post }
  type User  { id: ID!, created: Int, username: String, bio: String, status: String, avatar: String, posts: [Post] }
  type Post { id: ID!, created: Int, user_id: Int, text: String, object_set_id: Int, edited: Int, views: Int, upvotes: Int, downvotes: Int, Parent: Int }
`;

// The resolvers
const resolvers = {
    Query: {
        user: (parent, args, context, info) => {
            return users.find(user => user.id === Number(args.id));
        },
        // posts: () => posts,
    },
    Post: {
        id: (parent, args, context, info) => {
            console.log(posts.filter(post => post.user_id === Number(parent.id)))
            return posts.filter(post => post.user_id === Number(parent.id))
        }
    }
};

I want to specify a user by their user ID, then filter down to get all the posts that user has made (eventually with a time filter).

I've tried several things, but 'posts' under a user always comes up 'null'. What am I missing? Thanks.

Upvotes: 0

Views: 1800

Answers (1)

Ezra Siton
Ezra Siton

Reputation: 7781

User (Or more semantic: userById) query

Your first query works fine. You should also add some data(Otherwise you get an error "message": "users is not defined",).

enter image description here

Execute query:

query{
  user(id: 1){
    id
    username
  }
}

Basic code example:

const { ApolloServer, gql } = require("apollo-server");

const users = [
  {
    id: 1,
    username: "Kate Chopin"
  },
  {
    id: 2,
    username: "Paul Auster"
  }
];

// The GraphQL schema in string form
const typeDefs = gql`
  type Query {
    userById(id: ID!): User
  }
  type User {
    id: ID!
    username: String
  }
`;

// The resolvers
const resolvers = {
  Query: {
    userById: (parent, args, context, info) => {
      return users.find((user) => user.id === Number(args.id));
    }
  }
};

const server = new ApolloServer({
  typeDefs,
  resolvers
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

posts query (Or more semantic: postById):

About posts - your query is posts and no resolver match + looks like you do a little "salad" their.

It is better to follow this example (Same idea only books/authors instead of posts/user): https://www.apollographql.com/docs/apollo-server/getting-started/

Next, read this Resolver chains: https://www.apollographql.com/docs/apollo-server/data/resolvers/#resolver-chains

Again hello world example


const posts = [
  {
    id: 1,
    name: "Article One"
  },
  {
    id: 2,
    name: "Article Two"
  }
];

const users = [
  {
    id: 1,
    username: "Kate Chopin",
    posts: posts
  },
  {
    id: 2,
    username: "Paul Auster"
  }
];

// The GraphQL schema in string form
const typeDefs = gql`
  type Query {
    userById(id: ID!): User
  }
  type User {
    id: ID!
    username: String
    posts: [Post]
  }
  type Post {
    id: ID!
    name: String
  }
`;

// The resolvers
const resolvers = {
  Query: {
    userById: (parent, args, context, info) => {
      return users.find((user) => user.id === Number(args.id));
    }
  }
};

const server = new ApolloServer({
  typeDefs,
  resolvers
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

Query: enter image description here

Upvotes: 1

Related Questions