Setup an Apollo GraphQL Server with automatic Mocking

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

When iterating on new GraphQL schema, it is great to have a server implementation allowing you to only provide type definitions and the server returning mocked results. This enables you to quickly iterate on ideas and verify them with example queries.

Apollo Server offers this by enabling the mocked mode. In this lesson create a new server with mocking enabled, customize mocks and walk through how to combine mocks with resolvers.

This setup then will be the basis for our schema design explorations. Let me add that while Apollo Server works well for this case, any other GraphQL server might do as well.

Instructor: [00:00] For this course, any GraphQL server would do because I'm quite familiar with it, we're going to use Apollo server. Usually, Apollo server requires you to define the type definitions in the GraphQL schema language and to the servers.

[00:15] For this course though, we're mostly not going to write your servers and simply mock the schema. This allows us to iterate faster when assigning the schema. We can activate the mock mode by passing in the option mocks and set it to true.

[00:31] If we now run Yarn start, the server will spin off and under local host:4000, we provide us with a GraphQL ID. Let's visit the URL in a browser and run a query. As you can see, our server is running and now the query was mocked.

[00:51] The Apollo server under mock mode by default will mock any type but it also gives us the opportunity to customize the mocks per type. Let's return just Hello for string instead of the Hello World. Therefore, we create a new object mocks containing one property. Its key is string and the value, a function returning Hello.

[01:15] Be aware that the GraphQL schema type string starts with an uppercase S and therefore, the object key has to do as well, then we replace the mocks Boolean with the full object to a name punning in JavaScript, we can simply remove the colon true.

[01:32] If we rerun the query, we can see that our string is now mocked with just Hello. Having paths of the schema mock, but other paths being actually resolved is also possible. We extend our type definitions with Hello to colon string and implement the resolve for it.

[01:56] We pass through servers object to Apollo server and the mutations set mock and entire schema to false. Now, we can create for Hello 2 and Hello. As you can see, Hello 2 returns the resolved result while Hello still relies on the mocks. This setup should take you quite far when exploring and iterating on a new schema design.

egghead
egghead
~ 10 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today