Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hello World 2018 - GraphQL A query language for your API

Hello World 2018 - GraphQL A query language for your API

Hello World Tech Conference

February 15, 2018
Tweet

More Decks by Hello World Tech Conference

Other Decks in Programming

Transcript

  1. 2 { "data": { "speaker": { "firstName": “Alexis", "lastName": “Mas",

    "role": “Software Engineer", "company": “XING", "twitter": “@_Axxiss“ } } } query me { speaker { firstName lastName role company twitter } }
  2. 5 GraphQL is a query language for APIs and a

    server-side runtime for fulfilling those queries with your existing data.
  3. 12 query Person { person(id: "cGVvcGxlOjE=") { name homeworld {

    name } } } A query and its response { "data": { "person": { "name": "Luke Skywalker", "homeworld": { "name": "Tatooine" } } }, "errors": null, "extensions": null }
  4. 14 query Person { person(id: "cGVvcGxlOjE=") { name homeworld {

    name } } } Operation definition Anatomy of a query
  5. 15 query Person { person(id: "cGVvcGxlOjE=") { name homeworld {

    name } } } Operation definition Argument name & value Anatomy of a query
  6. 16 query Person { person(id: "cGVvcGxlOjE=") { name homeworld {

    name } } } Operation definition Argument name & value Selection Anatomy of a query
  7. 17 Operation definition Argument name & value Selection Anatomy of

    a query query Person { person(id: "cGVvcGxlOjE=") { name homeworld { name } } }
  8. 18 Running a query query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } POST https://a-graphql-service.com/api { "query": "query Person { … }" }
  9. 19 POST https://a-graphql-service.com/api { "query": "query Person { … }"

    } Running a query { "data": { "person": { "name": "Luke Skywalker", "homeworld": { "name": "Tatooine" } } }, "errors": null, "extensions": null }
  10. 20 Ask what you need, get exactly that query Person

    { person(id: "cGVvcGxlOjE=") { name homeworld { name } } } { "data": { "person": { "name": "Luke Skywalker", "homeworld": { "name": "Tatooine" } } }, "errors": null, "extensions": null }
  11. 21 Let’s use variables •Avoid string manipulation •Use dynamic values

    •Reuse existent queries query Person { person(id: "cGVvcGxlOjE=") { name homeworld { name } } }
  12. 23 $Variables query Person($personID: ID!) { person(id: $personID) { name

    homeworld { name } } } Variable definition Variable reference
  13. 24 $Variables query Person($personID: ID!) { person(id: $personID) { name

    homeworld { name } } } { "personID": "cGVvcGxlOjE=" } Variable definition Variable reference Variable value
  14. 25 $Variables query Person($personID: ID!) { person(id: $personID) { name

    homeworld { name } } } { "personID": "cGVvcGxlOjE=" } Variable definition Variable reference Variable value
  15. 26 Running a query with variables POST https://a-graphql-service.com/api { "query":

    "query Person($pers…", "variables": { "personID": "cGVvcGxlOjE" } } { "data": { "person": { "name": "Luke Skywalker", "homeworld": { "name": "Tatooine" } } }, "errors": null, "extensions": null }
  16. 27 Lets break it! query Person($personID: ID!) { pessoa(id: $personID)

    { name homeworld { name } } } { "personID": "cGVvcGxlOjE=" }
  17. 28 Something went wrong… { "errors": [ { “message": "Cannot

    query field \"pessoa\" on type \”Root\”. Did you mean \”person\"?", "locations": [ { "line": 16, “column": 3 } ] } ] }
  18. 29

  19. 30 Mutation mutation UpdateName($input: UpdateNameInput) { updatePerson(input: $input) { name

    homeworld { name } } } { “input”: { id: “cGVvcGxlOjE=“, name: “Korl Marcus“} }
  20. 31 Just another response { "data": { "person": { "name":

    “Korl Marcus”, "homeworld": { "name": "Tatooine" } } }, “errors": null, “extensions”: null }
  21. 35

  22. 36 Client Engine Source (1) Send request (5) Send response

    (2) Validate request (3) Get data (3) Get data (4) Aggregate data
  23. 38 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] }
  24. 39 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] }
  25. 40 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] }
  26. 41 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] }
  27. 42 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] }
  28. 43 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] }
  29. 44 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] } Scalars
  30. 45 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] } Scalars Objects
  31. 46 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] } Scalars Objects Non-null
  32. 47 The type system query Person { person(id: "cGVvcGxlOjE=") {

    name homeworld { name } } } type Query { person(id: ID!): Person! } type Person { id: ID! name: String height: Int homeworld: Planet } type Planet { id: ID! name: String climates: [String] } Scalars Objects Non-null Lists
  33. 49 Wrapper types ID can be null? List can be

    null? ID YES What list? ID! NO What list? [ID] YES YES [ID!] NO YES [ID]! YES NO [ID!]! NO NO
  34. 50 Client Engine Source (1) Send request (5) Send response

    (2) Validate request (3) Get data (3) Get data (4) Aggregate data
  35. 55