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

Live de GraphQL - Lukin

Avatar for Brito Brito
August 20, 2020
29

Live de GraphQL - Lukin

Avatar for Brito

Brito

August 20, 2020

Transcript

  1. RESULT 1 book id: title
 { ( ) {
 }

    } "Harry Potter and 
 the Chamber of Secrets" "book" "title" "data" { : { : { : } } }
  2. RESULT 3 book id: title
 { ( ) {
 }

    } /* other things */ "Book not found!" "book" null "book" "message" "locations" "line" "column" "path" "extensions" "data" "errors" 2 3 { : { : }, : [{ : , : [ { : , : } ], : [ ], : { }, }] }
  3. Exemplos de Erros Unauthorized User Reserved Book Internal Server Error

    Bad Gateway Nada deu errado Algo deu errado Book Not Found
  4. Exemplos de Erros Book Not Found Unauthorized User Reserved Book

    Internal Server Error Bad Gateway Resultados Erros
  5. { : } String
 User username type String message BookNotFound

    { : } type BookResult book id Query ID! { ( : ): } type Book ReservedBook BookNotFound BookResult = | | union ID String String
 id title author Book { : : : } type ID String String User id title author reservedBy ReservedBook { : : : : } type
  6. "Harry Potter and the Chamber of Secrets" "J.K. Rowling" "@brito"

    "Jurassic Park" "Michael Crichton" 1 2 id title author reservedBy username id title author books = [ { : , : , : , : { : , }, }, { : , : , : , }, ]; const
  7. "BookNotFound" "ReservedBook" "Book" __resolveType if return if return return :

    { ( ) { ( . ) { ; } ( . ) { ; } ; }, } message reservedBy BookResult book book book
  8. "Book not found!" book => const find => Number if

    ! return return : { : ( , { }) { = . (( ) . === ( )); ( ) { { : , }; } ; }, } message id Query parent id foundBook books book book id foundBook foundBook
  9. RESULT "BookNotFound" "Book not found!" "book" "__typename" "message" "data" {

    : { : { : , : } } } on on on Book ReservedBook BookNotFound 3 book id __typename id title author id title author reservedBy username message { ( : ) { ... { } ... { { } } ... { } } }
  10. RESULT "Book" "2" "Jurassic Park" "Michael Crichton" "book" "__typename" "id"

    "title" "author" "data" { : { : { : , : , : , : } } } on on on Book ReservedBook BookNotFound 2 book id __typename id title author id title author reservedBy username message { ( : ) { ... { } ... { { } } ... { } } }
  11. RESULT "ReservedBook" "1" "Harry Potter and the Chamber of Secrets"

    "J.K. Rowling" "@brito" "book" "__typename" "id" "title" "author" "reservedBy" "username" "data" { : { : { : , : , : , : , : { : } } } } on on on Book ReservedBook BookNotFound 1 book id __typename id title author id title author reservedBy username message { ( : ) { ... { } ... { { } } ... { } } }
  12. Book ReservedBook Error BookResult = | | union String ErrorType

    errorMessage type Error { : : } type NOTFOUND UNAUTHENTICATED ErrorType { } enum
  13. "Error" "ReservedBook" "Book" __resolveType if return if return return :

    { ( ) { ( . ) { ; } ( . ) { ; } ; }, } errorMessage reservedBy BookResult book book book
  14. "Book not found!" "NOTFOUND" book => const find => Number

    if ! return return : { : ( , { }) { = . (( ) . === ( )); ( ) { { : , : , }; } ; }, } errorMessage type id Query parent id foundBook books book book id foundBook foundBook
  15. Diretivas O que é? Uma diretiva é um identificador precedido

    por um caracter @, opcionalmente seguido por uma lista de argumentos nomeados, que podem aparecer após quase todas as formas de sintaxe do GraphQL
  16. Diretivas O que posso fazer com isso? Uma vez que

    a especificação GraphQL não discute qualquer estratégia específica de implementação de diretivas, cabe a cada estrutura de servidor GraphQL expor uma API para implementação de novas diretivas.
  17. schema scalar object field argument iface union type value object

    field ( : GraphQLSchema) ( : GraphQLScalarType) ( : GraphQLObjectType) ( : GraphQLField<any, any>) ( : GraphQLArgument) ( : GraphQLInterfaceType) ( : GraphQLUnionType) ( : GraphQLEnumType) ( : GraphQLEnumValue) ( : GraphQLInputObjectType) ( : GraphQLInputField) visitSchema visitScalar visitObject visitFieldDefinition visitArgumentDefinition visitInterface visitUnion visitEnum visitEnumValue visitInputObject visitInputFieldDefinition
  18. "apollo-server" SchemaDirectiveVisitor AuthDirective SchemaDirectiveVisitor field AuthDirective // function implementation {

    } = ( ); { ( ) { } } . = { }; const require class extends visitFieldDefinition module exports
  19. errorMessage type "valid-token" "User isn't authenticated" "UNAUTHENTICATED" resolve resolve token

    token field defaultFieldResolver field field args context args context context resolve this args ( ) { { = } = ; . = (... ) { [, , ] = ; (! . || . !== ) { { : , : , }; } . ( , ); }; } visitFieldDefinition const async function const if return return apply
  20. headers authorization typeDefs resolvers schemaDirectives context token server ApolloServer auth

    AuthDirective req req = ({ , , : { : , }, : ({ }) ({ : . . , }), }); const new =>
  21. RESULT "Error" "User isn't authenticated" "UNAUTHENTICATED" "book" "__typename" "errorMessage" "type"

    "data" { : { : { : , : , : } } } on on on Book ReservedBook Error 2 book id __typename id title author id title author reservedBy username errorMessage type { ( : ) { ... { } ... { { } } ... { } } }