Skip to content

Instantly share code, notes, and snippets.

@kdipaolo
Last active October 15, 2018 00:35
Show Gist options
  • Save kdipaolo/df946a336f69cda06689e5582b8f561e to your computer and use it in GitHub Desktop.
Save kdipaolo/df946a336f69cda06689e5582b8f561e to your computer and use it in GitHub Desktop.
graphql-apollo-server-tutorial.md

#GraphQL

Apollo Server

can be used with several popular libraries for Node.js: Express, Koa, Hapi. Apollo itself is kept library agnostic, so it is possible to connect it with a lot of third-party libraries in client but also server applications.

GraphQL Yoga (Made by Prisma)

An abstraction around apollo, graphql, express to give you everything you need to create a graphql server

Express

Middleware library for Node.js.

Two parts to a graqph ql server: Schema and resolvers. GraphQL is a type based thin layer over an api. Think typescript for your api. A schema is a description of what your api should look like.

input type is a specidal type that can only be used for arguments

# input example
input NewPersonInput {
  name: String!
}

type mutation {
  newPerson(input: NewPersonInput!): Person!
} 

A type is a custom shape that describles fields and what types of data those fields have "!" means can never be null

GraphQL Schemas

The GraphQL schema is all the available data for reading (and writing) data via GraphQL. It can happen from any client who consumes the GraphQL API. The schema consists of type definitions, starting with a mandatory top level Query type for reading data (type Query), and then followed by fields and nested fields.

const schema = gql`
  # Mandatory root Query type
  type Query {
    # me field with type User (Which means it must have a username subfield with type String
    me: User
  }
  # Custom type definition
  type User {
    username: String!
  }
`;

GraphQL Resolvers

The counterpart of the GraphQL schema for setting up a Apollo Server are the resolvers which are used to return data for your fields from the schema. The data source doesn’t matter, because the data can be hardcoded , can come from a database, or from another (RESTful) API endpoint. That’s why GraphQL shouldn’t be mistaken for a database query language. Resolvers are only functions which resolve data for your GraphQL fields in the schema.

 Query: {
    # Resolver function name must match the field name in the schema query (me)
    me: () => {
      # the return value of the function must match the schema of the query return type (User)
      return {
        username: 'kdipaolo',
      };
    },
  },

Client Side Query

In the client side query we can write:

{
  me {
    username
  }
}

and get the following data returned:

{
  "data": {
    "me": {
      "username": "kdipaolo"
    }
  }
}

Enum's

Also called Enums, enumeration types are a special kind of scalar that is restricted to a particular set of allowed values.

enum FoodTypes {
  burger
  nachos
  tacos
}

type Person {
  id: ID!
  name: String!
  favoriteFood: FoodTypes
}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment