TODO

  • API - single source of truth
  • Schema-first vs. Code-first
  • openapi
  • graphql
  • grpc

API-first

What does that mean?

I built the frontend code but backend was not ready yet. I needed to use mocks to test it. After backend was completed I had to rewrite some parts.
- a frustrated engineer😤

Does that sound familiar?

Define API before starting implementation

API-first

  • 💯 Single Source Of Truth
  • 💻 Code Generation
  • ⌨️ Typing & Validation

Two flavors

  1. Schema-first
  2. Code-first

Schema-first

Define API schema in a standard format

Can be used with and transformed into any language

schema {
  query: Query
}
type Query {
  get(id: ID!): Product
}
type Product {
  id: ID! @id
  name: String! @column
}
query getProduct($id: ID!) {
  product(id: $id) {
    id
    name
  }
}
							
Example: GraphQL

Code-first

Define API schema in a specific language

Can be used from any client that supports the format

import graphene

class Query(graphene.ObjectType):
    name = graphene.String()

    def resolve_get_product(root, info, id):
        product = get_product_from_db(id)
        return {
            'id': product.id,
            'name': product.name,
        }

schema = graphene.Schema(query=Query)
							
Example: Graphene (Python GraphQL)

Code-first binds you to a server language

Schema-first might involve additional steps to create the server