theonlychase
10/16/2019 - 5:08 PM

GraphQL Notes

Creating Schemas with SDL

  1. Schema Definition Language

    1. Instead of functions, uses a verbose, string based syntax for schemas
    2. Can transform the syntax into many representations if needed
    3. much easier to read
    4. composable
  2. Scalar and Object Types

    1. Describe resources that will be used in queries and mutations
  3. Scalar Types

    1. Built in primitives
      1. String
      2. Int
      3. Float
      4. Boolean
      5. ID
  4. Object Types

    1. Custom shapes with fields that are either scalar types or other object types
    2. Object type fields also describe any arguments and or validations
    3. Types are the target or all requests. Obj of gql is to resolve types
  5. Query and Mutations Types

    1. CRUD on your GraphQL API
    2. Query types describe the different queries your API is capable of
      1. You can define types, but if they aren't named inside the query type, its useless
    3. A querry opration is just a name, with possible arguments that eventualy return a type
    4. A mutation is the same, but with the intent of mutating the DB vs just reading
    5. Queries and Mutations are what will be available to clients with your API.
      1. Think of them as your routes. They're what your API can do
  6. Resolvers

    1. Like controllers, but instead resolve types all the way down. Where you talk to the DB
    2. Responsible for retrieving data
    3. Every query and mutation you have, you must have a resolver that returns the specified type
    4. Types and fields on types often hae resolvers as well
    5. Incoming query dictates what resolvers run and in what order
  7. Creating Resolvers

    1. Return the same shape as described in the schema, or delegate to another resolver
    2. Resolvers take a few args
      1. starting obj (what the parent resolveer returned or starting value from the server)
      2. args (any args fron the incoming request)
      3. context (shared context obj across all resolvers, like the req obj in express)
      4. info (advanced AST of the incoming request) - abstract syntax tree
  8. Interfaces

    1. Inheritable types for your schema
    2. Some types are very similar with the exception of a few fields
    3. You can use an interface as a base type and have other types implement that interface
    4. You then have to use fragments in your request query to conditionally ask for that type specific fields
  9. Unions

    1. Combo type that can be one of many different ty[es that may not relate to each other
    2. Smetimes you want a query to return a possibility of more than just one type. Unions allow you to create a type that is composed of many types where any of them may be fulfilled.
    3. Great for search queries