--- title: Schema Design description: The best way to fetch data, update it, and keep things running for a long time --- GraphQL schemas are at their best when they are designed around the need of client applications, instead of the shape of how the data is stored. Often times teams will create schemas that are literal mappings on top of their collections or tables with CRUD like root fields. While this may be a fast way to get up and running, a strong long term GraphQL schema is built around the products usage. ## Style conventions The GraphQL specification is flexible in the style that it dictates and doesn't impose specific naming guidelines. In order to facilitate development and continuity across GraphQL deployments, we suggest the following style conventions : - **Fields**: are recommended to be written in `camelCase`, since the majority of consumers will be client applications written in JavaScript. - **Types**: should be `PascalCase`. - **Enums**: should have their name in `PascalCase` and their values in `ALL_CAPS` to denote their special meaning. ## Using interfaces Interfaces are a powerful way to build and use GraphQL schemas through the use of _abstract types_. Abstract types can't be used directly in schema, but can be used as building blocks for creating explicit types. Consider an example where different types of books share a common set of attributes, such as _text books_ and _coloring books_. A simple foundation for these books might be represented as the following `interface`: ```graphql interface Book { title: String author: Author } ``` We won't be able to directly use this interface to query for a book, but we can use it to implement concrete types. Imagine a screen within an application which needs to display a feed of all books, without regard to their (more specific) type. To create such functionality, we could define the following: ```graphql type TextBook implements Book { title: String author: Author classes: [Class] } type ColoringBook implements Book { title: String author: Author colors: [Color] } type Query { schoolBooks: [Book] } ``` In this example, we've used the `Book` interface as the foundation for the `TextBook` and `ColoringBook` types. Then, a `schoolBooks` field simply expresses that it returns a list of books (i.e. `[Book]`). Implementing the book feed example is now simplified since we've removed the need to worry about what kind of `Book`s will be returned. A query against this schema, which could return _text books_ and _coloring_ books, might look like: ```graphql query GetBooks { schoolBooks { title author } } ``` This is really helpful for feeds of common content, user role systems, and more! Furthermore, if we need to return fields which are only provided by either `TextBook`s or `ColoringBook`s (not both) we can request fragments from the abstract types in the query. Those fragments will be filled in only as appropriate; in the case of the example, only coloring books will be returned with `colors`, and only text books will have `classes`: ```graphql query GetBooks { schoolBooks { title ... on TextBook { classes { name } } ... on ColoringBook { colors { name } } } } ``` To see an interface in practice, check out this [example]() ## A `Node` interface A so-called "`Node` interface" is an implementation of a generic interface, on which other types can be built on, which enables the ability to fetch other _types_ in a schema by only providing an `id`. This interface isn't provided automatically by GraphQL (not does it _have_ to be called `Node`), but we highly recommend schemas consider implementing one. To understand its value, we'll present an example with two collections: _authors_ and _posts_, though the usefulness of such an interface grows as more collections are introduced. As is common with most database collections, each of these collections have unique `id` columns which uniquely represent the individual documents within the collection. To implement a so-called "`Node` interface", we'll add a `Node` interface to the schema, as follows: ```graphql interface Node { id: ID! } ``` This `interface` declaration has the only field it will ever need: an `ID!` field, which is required to be non-null in all operations (as indicated by the `!`). To take advantage of this new interface, we can use as the underlying implementation for the other types that our schema will define. For our example, this means we'll use it to build `Post` and `Author` object types: ```graphql type Post implements Node { id: ID! title: String! author: Author! } type Author implements Node { id: ID! name: String! posts: [Post] } ``` By implementing the `Node` interface as the foundation for `Post` and `Author`, we know that anytime a client has obtained an `id` (from either type), we can send it back to the server and retrieve that exact piece of data back!