---
# System prepended metadata

title: Graphql Demo

---

# Graphql Demo

## What it is
  * Query language
  * Not a storage engine
  * Not a framework (but frameworks exists for almost any language)

## Queries
  * The format of the query dictates the format of the response
  * Field arguments to supply parameters or work with pagination

```graphql=
query GetCampaigns {    
  campaigns {
    id
    title
  }
}
```

## Mutations

```graphql=
mutation CreateCampaign {
  campaignCreate(title: "My new campaign", gameId: "abc-123") {
    id
  }
}

```

## Schema validation
  * If the type system says it's ok, then it's ok (barring runtime errors on the server)

```graphql=
campaigns {
    id
    name <-- ERROR, no field named 'name' on type 'Campaign'
}

campaignCreate(title: 123) { <-- invalid type given for 'title', should be String
    
}
```


## Composition
  * Nest queries, resolvers will do all the heavy lifting
  * Get the data you need, No over/under fetching
  * No handling multiple endpoints
  * One single request to the server
  * N+1 can be mitigated by data loading i.e. batch fetching relations
  * A type is always resolved the same way (so Sponsorship -> Broadcaster is the same as Broadcasters[])

```graphql=

query GetCampaigns {
  campaigns {
    id
    title
    sponsorships {
      id
      commission
      broadcaster {
        name
      }
    }
  }
}

```


## Local types based on the query

```typescript=
type Campaign = GetCampaignsQuery['campaign'][0]


export class MyComponent() {
  
  campaigns$: Observable<Campaign[]>
  constructor(private getCampaigns: GetCampaignsGQL) {}
  
  ngOnInit() {
    this.campaigns = getCampaigns();
  }

}

```


## Resolvers
  * "Resolve" a field or property on a model
  * does not need to map to a db model, can expose computed data or anything that is too complex for the client to do.
  * Can resolve to other resolvers
    * Easier to handle complex relations since you only need to write resolve the fields of your own model, and you can let other resolvers handle their own things

## Codegen
  * SDK
  * Types

## Playground
  * Test queries out
  * Works as documentation

## Self documenting
  * Using introspection in vs code to provide intellisense when writing queries that is automatically synched with the server definitions thanks to codegen
