# Introducing Code-First GraphQL APIs at Cambridge University Press

By Jag Reehal, 2021-02-22. Canonical: https://arrangeactassert.com/posts/introducing-code-first-graphql-apis-at-cambridge-university-press/

<!-- Excerpt Start -->

Having experienced the pain of schema-first [GraphQL](https://graphql.org/) projects, I wanted to use a different approach for a project I led at Cambridge University Press.

In this post, I'll describe what I used and how using code-first GraphQL worked out for us.

<!-- Excerpt End -->

<div>

<svg aria-hidden="true" class="h-64 w-64 float-left pr-12" style="shape-outside: circle(50%)" viewBox="0 0 256 288">
<title>GraphQL logo</title>
<path d="M152.576 32.963l59.146 34.15a25.819 25.819 0 015.818-4.604c12.266-7.052 27.912-2.865 35.037 9.402 7.052 12.267 2.865 27.912-9.402 35.037a25.698 25.698 0 01-6.831 2.72v68.325a25.7 25.7 0 016.758 2.702c12.34 7.125 16.527 22.771 9.402 35.038-7.052 12.266-22.771 16.453-35.038 9.402a25.464 25.464 0 01-6.34-5.147l-58.786 33.94a25.671 25.671 0 011.295 8.08c0 14.103-11.458 25.636-25.635 25.636-14.177 0-25.635-11.46-25.635-25.636 0-2.52.362-4.954 1.037-7.253l-59.13-34.14a25.824 25.824 0 01-5.738 4.52c-12.34 7.051-27.986 2.864-35.038-9.402-7.051-12.267-2.864-27.913 9.402-35.038a25.71 25.71 0 016.758-2.703v-68.324a25.698 25.698 0 01-6.831-2.72C.558 99.897-3.629 84.178 3.423 71.911c7.052-12.267 22.77-16.454 35.037-9.402a25.82 25.82 0 015.79 4.575l59.163-34.159a25.707 25.707 0 01-1.048-7.29C102.365 11.46 113.823 0 128 0c14.177 0 25.635 11.459 25.635 25.635 0 2.548-.37 5.007-1.059 7.328zm-6.162 10.522l59.287 34.23a25.599 25.599 0 002.437 19.831c3.609 6.278 9.488 10.44 16.013 12.062v68.41c-.333.081-.664.17-.993.264L145.725 44.17c.234-.224.464-.452.689-.684zm-36.123.7l-77.432 134.11a25.824 25.824 0 00-1.01-.27v-68.417c6.525-1.622 12.404-5.784 16.013-12.062a25.6 25.6 0 002.427-19.869l59.27-34.22c.239.247.483.49.732.727zm24.872 6.075l77.414 134.08a25.492 25.492 0 00-4.513 5.757 25.7 25.7 0 00-2.702 6.758H50.64a25.71 25.71 0 00-2.704-6.758 25.825 25.825 0 00-4.506-5.724l77.429-134.107A25.715 25.715 0 00128 51.27c2.487 0 4.89-.352 7.163-1.01zm11.795 194.478l58.902-34.008a25.865 25.865 0 01-.473-1.682H50.607a25.87 25.87 0 01-.266.992l59.19 34.175A25.558 25.558 0 01128 236.373a25.564 25.564 0 0118.958 8.365z" fill="#E535AB" fill-rule="evenodd"/>
</svg>

In my experience, having a single source of truth makes applications easier to understand, maintain and add features to.

A common approach when learning GraphQL is to adopt a schema-first approach. The difficulty and complexity of schema-first GraphQL APIs increases as the project and teams grow, as you have to ensure definitions and resolvers are in sync and have to maintain multiple sources of truth.

</div>

## Code-first approach with Nexus

By using a code-first approach with [Nexus](https://nexusjs.org/), we were able to can have have a single source of truth as it will generate a GraphQL schema for you.

However Nexus does a lot more than saving from mistakes and time of generating a GraphQL scheme manually.

Thanks to excellent typescript integration Nexus provides a delightful developer experience with lightning-fast feedback.

The plugin system and extensibility points enabled us to create a GraphQL API that was easy to maintain and extend.

## How how did Nexus and code-first GraphQL work out for us?

Having avoided the pitfalls of schema-first, the benefits of a code-first approach to building GraphQL APIs increased our productivity and enabled us to release features quickly knowing everything was in sync.

Over the next few months, I'll post examples of how we used Nexus and share what we learned when creating GraphQL APIs.