--- 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]() ## Node interface Given the power of interfaces, one pattern that can add a safe layer of flexibility to our schema is the `Node` interface pattern. We really recommend all schemas to follow this pattern if possible! The `Node` interface provides a way to fetch potentially any type in our schema with just an `id` field. We will explain how it works though a common example: Say we have a database with two different tables; `Author` and `Post`. Each of these tables have an `id` column that is unique for that table. To use the `Node` interface we would add the following to our schema: ```graphql interface Node { id: ID! } ``` This is the actual `Node` interface. It has only one field which is an `ID!`, meaning it is a schema unique string that is required to exist. To use the `Node` interface in our example, we would write our types like so: ```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, we know that anytime we have an `id` field from either `Author` or `Post`, we can send it back to our server and retreive that exact piece of data back! But earlier we said our database has ids that are unique only to each table, so how is this possible?