1.1.1-rev.6 • Published 3 years ago
@dosvit/api-gateway v1.1.1-rev.6
To use with local development services, add LOCAL=true
to .env
and list your services in services.json
Pull data from another service
Users
pulls books
by adding book to a user:
# BooksBook MUST have the same structure as external type
# reduced to key fields (e.g. {id} or {topic: {id}}) and, optionally, denrmalized fields
type User @key(fields: "id") {
id: ID!
name: String
book: BooksBook
}
# This will use federation _entities(representations: $representations) query
# using external type's key
type BooksBook @external {
id: ID!
# optional back link. `fields` supports graphql syntax with aliases, can be use fields, leaf properties will be used as vars in `representation`
# e.g. @inverse(fields: "title publisher {publisherId: id}", representation: "book: {title: $title, publisher_id: $publisherId}")
# also supports custom queries via `query` prop
# e.g. @inverse(fields: "boookId: id", representation: "$boookId", query: "users(where: {book_id_in: $representations})")
user: User @inverse(fields: "id", representation: "book: {id: $id}")
}
# This will use federation _entities(representations: $representations) query
# using explicity set key in `fields` (can be complex, such as "topic {id}, uses graphql selection syntax")
type BooksBook @external(fields: "id") {
id: ID!
}
# This will use custom query from related service and pass it's arguments to this service
# query has two predefined variables that can be used: $representations and $typename
# $typename will be replaced with nrmalized type (`Book` in this example)
# in $representations will be injected result of querying `fields` from stored representations
# e.g. parent user `{"id": 1. "book": {"id": 1}}`, `fields: "id"` will query `book` and result in `{"id": 1}`
# optional `onlyKeys: true` will extract values from representation, and result in `1`
# field arguments will be smartly merged with relation constraint
# LIMITATIONS:
# * query must be on top level
# * `$representations` will always be an array
type BooksBook @external(query: "Books(where: {id_in: $representations})", fields: "id", onlyKeys: true) {
id: ID!
}
Push data to another service
Books
pushes itself to users
by adding book to a user from itself
type Book @key(fields: "id") {
id: ID!
title: String
announce: String
}
extend type UsersUser @key(fields: "id") {
id: ID! @external
book: Book
}
dev tips
Homestead included. To run without php copy Homestead.yaml.example to Homestead.yaml and replace $PWD with current directory and change provider
if needed`. In bash:
cp Homestead.yaml.example Homestead.yaml && sed -i '' "s|\$PWD|$PWD|g" Homestead.yaml