2017 saw the creation of several high profile public GraphQL APIs. These GraphQL APIs are great for customer integrations, third-party integrators, professional services and internal consumption. When creating a public GraphQL API there are several important considerations.
Surface Area – Keep It Skinny
Deciding how much surface area to expose is important. Exposing a too-limited set of apis and properties can result in an API that isn’t usable by some of your key consumers. Exposing too many APIs and properties leaves you on the hook to support those APIs that you didn’t expect people to use, or in ways you didn’t expect them to be used. The approach we took was to keep it skinny. It isn’t just easier to add functionality when it becomes necessary. Keeping the API skinny gives you a growth path and prepares API consumers for the continuous evolution which all public APIs inevitably go through.
Naming Conventions – Consistency Matters
Locking down simple naming conventions early is very important. While the API is going to evolve, you don’t want to torture your customers with a continuous renaming of public APIs through new query/mutation names and the slow deprecation of old query/mutation names. The naming conventions we settled on were:
- createEntity, updateEntity, deleteEntity for mutation names
- saveEntity for complex entity operations (create it if it doesn’t exist or update if it already exists)
- entity to query an entity by id and entities to query entities using a variety of search criteria
- Entity for type definition names
- SearchEntity for the search criteria for a particular entity type
Deprecating Fields – Avoid Surprises
We use deprecated fields as a way to tag things that are going to go away. The goal should be to keep deprecated fields for a couple major releases after tagging them as deprecated so you don’t surprise consumers when they disappear. We also use deprecated fields as a way to tag experimental features. These are warnings that the API is very likely to change and/or might only be partially implemented. Again, the goal is to give consumers a hint so there are no surprises.
Architect For Reuse
Try to build architectural components that aid in resolving queries from similar data sources. In our case, 60% of our current functionality uses existing REST APIs under the covers. It was possible to build helpers to leverage the similarities that the REST APIs share so each query resolver, field resolver, sort order, etc. can leverage these reusable helpers instead of treating the implementation of each resolver as the first time. We were able to build architectural components to help getting data from REST, direct SQL, Cypher queries to Neo4J and even another GraphQL endpoint.
Think About Testing
I recently blogged about our approach for creating a GraphQL integration testing framework. Think about testing as you build out your API. We created unit tests as developed the API, schema snapshot tests to catch accidental schema changes, and eventually full GraphQL integration tests that are completely independent of external datasources. These tests will give you confidence that you haven’t broken things for your consumers as you iterate and improve your internal architecture.
Evangelize It
Teaching consumers about your awesome new GraphQL API is something you will want to do. What is the point of developing such a wonderful and easy to use API if your customers, third-party integrators, professional services or internal developers don’t use it? My advice for evangelizing any technology is the same – keep it simple. GraphQL is new and often misunderstood. Explain how your GraphQL API makes it super easy to get or manipulate data. Explain how the hierarchical nature of GraphQL query results saves consumers from having to stitch results from several REST or DB calls together on the client side. Keep it simple and approachable and pretty soon you’ll have consumers asking for enhancements, performance improvements and finding new and exciting ways to leverage your data!