User GraphQL Fields
In this module, we begin building the server and client implementation that will help allow us to retrieve and display information for users in our application. We'll begin by brainstorming the GraphQL fields we'll need to query user data.
To display information about our users in our client application, we'll need to have our client be able to query & resolve user data from our server. With that said, we'll brainstorm exactly what GraphQL fields we'll need to accomplish this.
For our application, we'll need a single root-level user
query since we'll want the client to query a single user at a time. This should be fairly straightforward to implement on the server since we'll only need to find a single user document from the users
collection, and return that document! The only complication with this field is that we'll look to return sensitive user data only when the user requests their own information. For example, we won't want to query the income
of another user.
In our GraphQL type definitions in the src/graphql/typeDefs.ts
file, let's create a new user
field in the root Query
object that is to resolve to a non-null string value.
import { gql } from "apollo-server-express";
export const typeDefs = gql`
#...
type Query {
#...
user: String!
}
#...
`;
Next, we'll set up the resolver function for the user
field we've created in our schema. Since user
isn't directly related to the concept of a viewer, we'll create a new resolvers map to contain the resolver function within the user module.
This page is a preview of TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two