Apollo Server
On the backend side, our first goal is to create a GraphQL server that can:
Receive an incoming GraphQL query from our client
Validate that query against our newly created schema
Populate the queried schema fields with mocked data
Return the populated fields as a response
The Apollo Server library helps us implement this server quickly, painlessly, and in a production-ready way.
In the server/src/
folder, open index.ts
.
To create our server, we'll use the @apollo/server
package that we installed previously. From that package, we'll only need the named export ApolloServer
, so we'll declare that constant between curly braces.
We'll also need to use the startStandaloneServer
function, which we can import from the @apollo/server/standalone
package.
Just below, we'll import our typeDefs
from our schema.ts
file:
Take note that we're importing from "./schema"
, without the .ts
extension! This is because ultimately our compiled code will be importing a JavaScript file, which means our code will look for a ".js"
file extension! We don't have a "schema.js"
file currently, so we can omit the file extension altogether and TypeScript will help us pull all the necessary pieces together when it compiles the code.
Next, let's set up an async
function called startApolloServer
. Inside, we'll create an instance of the ApolloServer
class and pass it our typeDefs
in its options object:
Note: We're using shorthand property notation with implied keys, because we've named our constant with the matching key (typeDefs
).
To start the server, we'll use the startStandaloneServer
function, passing it the server
we just initialized.
The startStandaloneServer
function returns a Promise
, so we'll await
the results of that call, and pull out the url
property from the result.
We'll also log a nice little message letting us know that our server is indeed up and running!
Finally, let's not forget to actually call the startApolloServer
function at the bottom of the file!
Save your changes. From the terminal, we'll launch our server with npm run dev
(make sure you're in the server/
folder).
We get the log message and...not much else! We have a running server, but that's it. Floating in the vacuum of localhost
space without access to any data, it's a sad and lonely server for now. 😿
Which of these are purposes of a GraphQL server?Exposing a separate endpoint for each schema typeReturning populated schema fields as a responseReceiving incoming GraphQL queriesCreating GraphQL queriesValidating GraphQL queries against our schemaCorrect. Well done 🚀🚀🚀
Even though our server isn't connected to any data sources yet, it would be great to be able to send the server a test query and get a valid response. Fortunately, ApolloServer
provides a way to do exactly that, using mocked data.
Mocking data
To enable mocked data, we'll need to use two new packages: @graphql-tools/mock
and @graphql-tools/schema
.
Let's go ahead and install them.
At the top, we'll import addMocksToSchema
and makeExecutableSchema
.
Then, we'll need to tweak the ApolloServer
initialization. Instead of passing it the typeDefs
directly, we'll be using the schema
property. This property is another way of initializing an Apollo Server, which is useful for building federated subgraphs (more on that in our Voyage series) or if we're using functions like makeExecutableSchema
(which we are!).
As the value of the schema
property, we'll call the addMocksToSchema
function and pass it an object. This object defines its own schema
property, and here we'll call the makeExecutableSchema
function. Then, we'll pass this function an object containing our typeDefs
.
With this code, we're generating an executable schema from our typeDefs
, and instructing Apollo Server to populate every queried schema field with a placeholder value (such as Hello World
for String
fields).
However, Hello World
isn't a very realistic value for the title of a track or the URL of an author's picture! To serve mocked data that's closer to reality, we'll define a mocks
object. This object contains functions that provide the mocked data we want the server to return for each queried field.
Here's our mocks
object:
This object defines mock values for all of the fields of a Track
object (including the Author
object it contains). We pass this object to the ApolloServer
constructor like so:
With mocks enabled, Apollo Server always returns exactly two entries for every list field.
To get more entries at a time, let's say 6, we'll add a Query.tracksForHome
to our mocks
object and return an Array of that given length like so: [...new Array(6)]
.
Now, with our server loaded with mocked data, how can we run a query on it to test if everything works as expected? In the next lesson, we'll use the Apollo Explorer to build and run test queries seamlessly.
Last updated