Day 1 of 5
⏱ ~60 minutes
GraphQL in 5 Days — Day 1

Schema, Types, and Your First Query

Set up Apollo Server, define types in SDL, write your first resolvers, and explore the API in GraphiQL.

What GraphQL Solves

With REST, you get what the server decides to give you. With GraphQL, the client specifies exactly what it needs. No over-fetching (getting more data than needed). No under-fetching (needing multiple requests to get everything).

Setup
npm init -y
npm install @apollo/server graphql
# Create server.js
server.js
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';

// Schema Definition Language (SDL)
const typeDefs = `#graphql
  type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]!
  }

  type Post {
    id: ID!
    title: String!
    body: String
    author: User!
  }

  type Query {
    users: [User!]!
    user(id: ID!): User
    posts: [Post!]!
  }
`;

const users = [
  { id: '1', name: 'Alice', email: '[email protected]' },
  { id: '2', name: 'Bob', email: '[email protected]' },
];

const resolvers = {
  Query: {
    users: () => users,
    user: (_, { id }) => users.find(u => u.id === id),
  },
  User: {
    posts: (user) => posts.filter(p => p.authorId === user.id),
  },
};

const server = new ApolloServer({ typeDefs, resolvers });
const { url } = await startStandaloneServer(server, { listen: { port: 4000 } });
console.log(`Server ready at ${url}`);
💡
The ! in GraphQL types means non-null. String! = never null. [Post!]! = non-null array of non-null posts. Use non-null as much as possible — it makes client code cleaner and prevents null checks everywhere.
📝 Day 1 Exercise
Build Your First GraphQL API
  1. C
  2. r
  3. e
  4. a
  5. t
  6. e
  7. a
  8. n
  9. A
  10. p
  11. o
  12. l
  13. l
  14. o
  15. S
  16. e
  17. r
  18. v
  19. e
  20. r
  21. w
  22. i
  23. t
  24. h
  25. U
  26. s
  27. e
  28. r
  29. a
  30. n
  31. d
  32. P
  33. o
  34. s
  35. t
  36. t
  37. y
  38. p
  39. e
  40. s
  41. .
  42. W
  43. r
  44. i
  45. t
  46. e
  47. r
  48. e
  49. s
  50. o
  51. l
  52. v
  53. e
  54. r
  55. s
  56. .
  57. R
  58. u
  59. n
  60. t
  61. h
  62. e
  63. s
  64. e
  65. r
  66. v
  67. e
  68. r
  69. a
  70. n
  71. d
  72. o
  73. p
  74. e
  75. n
  76. t
  77. h
  78. e
  79. G
  80. r
  81. a
  82. p
  83. h
  84. i
  85. Q
  86. L
  87. s
  88. a
  89. n
  90. d
  91. b
  92. o
  93. x
  94. a
  95. t
  96. l
  97. o
  98. c
  99. a
  100. l
  101. h
  102. o
  103. s
  104. t
  105. :
  106. 4
  107. 0
  108. 0
  109. 0
  110. .
  111. Q
  112. u
  113. e
  114. r
  115. y
  116. a
  117. l
  118. l
  119. u
  120. s
  121. e
  122. r
  123. s
  124. a
  125. n
  126. d
  127. a
  128. l
  129. l
  130. p
  131. o
  132. s
  133. t
  134. s
  135. .

Day 1 Summary

  • GraphQL has one endpoint. The client specifies what fields to return.
  • SDL defines the schema: types, fields, and their types. ! = non-null.
  • Resolvers are functions that return data for each field in the schema.
  • GraphiQL is the browser IDE for exploring and testing GraphQL APIs.
Finished this lesson?