#technology #engineering #integration #api #query #microservice

idea

GraphQL is a query language for APIs. It defines how the API can be called for query or mutating entities.

It's based on a set of type declarations[1] describing the entities that the API can serve, and their corresponding relationships.

Queries can then be made on these entities, and specify what needs to be retrieved[2] from the system, including from sub-types. GraphQL then returns it using json[3].

The GraphQL library itself is only a parser. Processing the queries is done using a resolver[4] which retrieves the data necessary to fulfill the request.

A GraphQL layer helps abstracting the underlying infrastructure and represents it under a more functional view. It also allows to compose data coming from multiple services as one, while providing a good framework to allow some relational querying across multiple services.

Some principles[3] to build a GraphQL API:

GraphQL federation[2] is a way to support integration to each of the services of a system and presenting a single Graph. This includes a single gateway but a service implementing the resolver for each of the domains.

links

references

examples

  type Query {
    person(id: ID): Person
  }

  type Person {
    id: ID
    firstName: String
    lastName: String
    notes: [Note]
  }

  type Note {
    id: ID
    content: String
    author: Employee
  }

  type Employee {
    id: ID
    name: String
    alias: String
  }

-[2]: Example of a query

query getPersonAndNotes{ 
  person(id: 4) {
    id
    firstName
    notes {
      content
      author {
        alias
      }
    }
  }
}
{
  "data": {
    "person": {
      "id": "4",
      "firstName": "Yurko",
      "notes": [
        {
          "content": "Note from Jordan about Yurko",
          "author": {
            "alias": "jfranco@"
          }
        },
        {
          "content": "Note from Maria about Yurko",
          "author": {
            "alias": "mdelan@"
          }
        },
        {
          "content": "Note from Piotr about Yurko",
          "author": {
            "alias": "pgut@"
          }
        },
        {
          "content": "Note from Sanah about Yurko",
          "author": {
            "alias": "spirte@"
          }
        }
      ]
    }
  }
}
class Resolver {
  person({ id }) {
    const person = data.persons[id]
    person.notes = () => this.notes(id)
    return person
  }

  notes(personId) {
    return data.notes
      .filter(note => note.personId === personId)
      .map(note => {
        const n = note
        n.author = () => this.employee({ id: n.employeeId })
        return n
      })
  }

  employee({ id }) {
    const employee = data.employees[id]
    return employee
  }
};