Publicité
Publicité

Contenu connexe

Publicité
Publicité

An intro to GraphQL

  1. An intro to GraphQL Suraj Paul https://www.drupal.org/u/suraj2012
  2. ● Data Query Language developed by Facebook in 2012. ● Released publicly in 2015. ● GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. ● GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data. ● Drupal 8 provides module to create and expose graphQL schema. https://www.drupal.org/project/graphql What is GraphQL ?
  3. Core Ideas of GraphQL ● Client Requests and Server payloads have same structure. ● The server contains the schema. ● The client dictates what it wants from the server to provide.
  4. Limitations of RESTful APIs ● Multiple Endpoints -- Endpoints specific to individual views ● Response Bloats -- Response contains more data, mostly unused. ● Many Round Trips -- Complex and relational data containing URLs. ● No backwards Compatibility -- REST APIs are versioned ● Not Introspective -- Lack Native Schema, making client side validation difficult
  5. ● Single Endpoint -- Single Endpoint can resolve GraphQL queries and send a single, unified response ● Tailored Response -- Response is catered with the client demand ● Fewer Round Trips -- Returns a single response flexible to accommodate many relationships ● Backward Compatibility -- common response for each version ● Introspective -- Native and Highly Extensible Schema How GraphQL Resolves this
  6. Operations GraphQL provide 2 types of operations ● Query ● Mutations Query: query { nodeById(id: 1) { title } }
  7. Fields can describe relationships with other data. Fields return values and can carry arguments. Arguments { nodeById(id: "1") { title } } Arguments
  8. Fragments GraphQL includes reusable units called fragments. Fragments let you construct sets of fields, and then include them in queries where you need to. fragment nodeFields on NodeArticle { nid type { targetId } created } { nodeQuery: nodeById(id: "2") { ...nodeFields } }
  9. Variables Sometimes we need to pass dynamic values to query. We can pass dynamic values using variables in GraphQL. query getArticle($nid: String!) { nodeById(id: $nid) { title } } { "nid": "2" }
  10. Mutation ● Most discussions of GraphQL focus on data fetching, but any complete data platform needs a way to modify server-side data as well. ● A module GraphQL Mutation is needed to perform POST operations. https://www.drupal.org/project/graphql_mutation ● The module Adds GraphQL mutations for all content entities.
  11. mutation ($input: NodeArticleCreateInput!) { createNodeArticle(input: $input) { violations { path message } errors entity { entityId entityBundle entityLabel } } } Mutation
  12. ● GraphQL in action in Drupal 8 Demo
  13. Future in Drupal ● Decoupled Drupal using React & graphQL . An example is given on https://github.com/fubhy/drupal-decoupled-app ● A number of modules supported by amazee.io. https://www.amazeelabs.com/en/blog/drupal-graphql-batt Eries-included ● Drupal and GraphQL with React and Apollo https://www.amazeelabs.com/en/blog/drupal-graphql-react-apollo
  14. Thank you
Publicité