5. Apollo Server
4m

🛠 Back-end first steps

On the back-end side, our first goal is to create a GraphQL server that can:

  1. Receive an incoming GraphQL query from our client
  2. Validate that query against our newly created schema
  3. Populate the queried schema fields with mocked data
  4. 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.js.

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 ApolloSever, so we'll declare that constant between curly braces. Just below, we'll import our typeDefs from our schema.js file:

const {ApolloServer} = require('apollo-server');
const typeDefs = require('./schema');

Next, we'll create an instance of the ApolloServer class and pass it our typeDefs in its options object:

const server = new ApolloServer({typeDefs});

Note: We're using shorthand property notation with implied keys, because we've named our constant with the matching key (typeDefs).

Finally, to start it up, we'll call the async listen method. When it resolves, it logs a nice little message letting us know that our server is indeed up and running:

server.listen().then(() => {
console.log(`
🚀 Server is running!
🔉 Listening on port 4000
📭 Query at https://studio.apollographql.com/dev
`);
});

Save your changes. From the terminal, we'll launch our server with npm run start (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?

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.

To enable basic mocked data, we could provide mocks:true to the ApolloServer constructor, like so:

const server = new ApolloServer({
typeDefs,
mocks: true
});

This instructs 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 pass an object to the mocks property instead of just true. This object contains functions that provide the mocked data we want the server to return for each queried field.

Here's our mocks object:

const mocks = {
Track: () => ({
id: () => 'track_01',
title: () => 'Astro Kitty, Space Explorer',
author: () => {
return {
name: 'Grumpy Cat',
photo:
'https://res.cloudinary.com/dety84pbu/image/upload/v1606816219/kitty-veyron-sm_mctf3c.jpg'
};
},
thumbnail: () =>
'https://res.cloudinary.com/dety84pbu/image/upload/v1598465568/nebula_cat_djkt9r.jpg',
length: () => 1210,
modulesCount: () => 6
})
};

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:

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

With mocks enabled, Apollo Server always returns exactly two entries for every list field. To get more entries at a time, we can use the MockList object that we import from apollo-server. Check out the docs for more details on MockList.

To use MockList, import it from apollo-server.

const {ApolloServer, MockList} = require('apollo-server');

Then, we can add a Query field to our existing mocks object.

const mocks = {
Query: () => ({
tracksForHome: () => new MockList([6, 9])
}),
Track: () => ({
id: () => 'track_01',
title: () => 'Astro Kitty, Space Explorer',
author: () => {
return {
name: 'Grumpy Cat',
photo:
'https://res.cloudinary.com/dety84pbu/image/upload/v1606816219/kitty-veyron-sm_mctf3c.jpg'
};
},
thumbnail: () =>
'https://res.cloudinary.com/dety84pbu/image/upload/v1598465568/nebula_cat_djkt9r.jpg',
length: () => 1210,
modulesCount: () => 6
})
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Which of these are true about querying Apollo Server without a connected data source?

Code Challenge!

Create a mock object with a type 'SpaceCat', an id of 'spacecat_01', and a title of 'spacecat pioneer'

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 Studio Explorer to build and run test queries seamlessly.

Previous
Next