Giter VIP home page Giter VIP logo

lilo's Introduction

Lilo Build Status Latest Release License

Lilo and Stitch

Lilo is a super-fast GraphQL stitching library. The project is heavily inspired by Atlassian Braid. Lilo focuses on simplicity and easy to use. You can find plenty of samples in the codebase and we aim to add more and more in every release. Please do not forget to check /samples directory.

We are planning to add additional frameworks supports. For now SpringBoot is supported. If you will use Lilo with SpringBoot. Please check Lilo Spring Documentation.

Installation

Just add a single Lilo dependency to your pom.xml file.

<dependencies>
  ...
  <dependency>
    <groupId>io.fria</groupId>
    <artifactId>lilo</artifactId>
    <version>23.12.1</version>
  </dependency>
  ...
</dependencies>

If you're using gradle add the dependency to your build.gradle file.

implementation 'io.fria:lilo:23.12.1'

Basic Usage

Here is the story, Alice has 2 GraphQL microservices and she wants to make her gateway to dispatch the GraphQL requests to their respective microservices. Microservice A provides a GraphQL query for user listing, and Microservice B provides a GraphQL mutation for user creation. So Alice can use both query and mutation via just sending requests to the Gateway directly. Lilo stitches the GraphQL schemas and provides a combined schema.

  +----------------+
  |                |
  | Microservice A | <---------+
  |                |           |            +----------------+
  +----------------+           |            |                |        o~
                               +----------- |    Gateway     | <---- /|\
  +----------------+           |            |                |       / \
  |                |           |            +----------------+
  | Microservice B | <---------+
  |                |
  +----------------+

A very basic working example for that scenario would be:

final Lilo lilo = Lilo.builder()
    .addSource(RemoteSchemaSource.create("SERVER_1", "https://server1/graphql"))
    .addSource(RemoteSchemaSource.create("SERVER_2", "https://server2/graphql"))
    .build();

But for a complete working example you need to serve the stitching logic from a GraphQL endpoint. (Probably /graphql) Please examine the 01-spring-boot-hello-world example in lilo-samples folder.

In most cases, we need an authentication or some sort of header manipulation. In that case, creating custom introspection and query retrievers might help us to modify outgoing requests or incoming responses. The following example shows a very basic example for custom retrievers.

    .addSource(
        RemoteSchemaSource.create(
            "SERVER_1",
            new MyIntrospectionRetriever("https://server1/graphql"),
            new MyQueryRetriever("https://server1/graphql")
        )
    )
    .addSource(
        RemoteSchemaSource.create(
            "SERVER_2",
            new MyIntrospectionRetriever("https://server2/graphql"),
            new MyQueryRetriever("https://server2/graphql")
        )
    )

For further details, you can examine the 02-spring-boot-basic-stitching example in lilo-samples folder.

If Gateway distributes the messages to Microservice A and also contains an embedded schema then the architecture might be something like this:


  +----------------+                       +----------------+
  |                |                       |                |        o~
  | Microservice A |---------------------- |    Gateway     | <---- /|\
  |                |                       |                |       / \
  +----------------+                       +----------------+
                                                  A    |
                                                  |    |
                                                  +<---+
final Lilo lilo = Lilo.builder()
    .addSource(
        RemoteSchemaSource.create(
            "SERVER_1",
            new MyIntrospectionRetriever("https://server1/graphql"),
            new MyQueryRetriever("https://server1/graphql")
        )
    )
    .addSource(
        DefinedSchemaSource.create(
            "SCHEMA_2",
            stringQueryDefinition,
            typeWiring
        )
    )
    .build();

We need to provide an IntrospectionRetriever and QueryRetriever. Those can fetch the query result from a remote source and/or local source.

After source definitions are properly set you can run your implementation similar to this:

final Map<String, Object> resultMap = lilo.stitch("{greeting1}").toSpecification();

Or more advanced GraphQL request which includes querym operation name and variables:

final GraphQLRequest graphQLRequest = GraphQLRequest.builder()
    .query(incomingGraphQlQuery)
    .operationName(incomingGraphQlOperationName)
    .variables(incomingGraphQlVariables)
    .build();

final Map<String, Object> resultMap = lilo.stitch(graphQLRequest.toExecutionInput()).toSpecification();

Parameter passing

Sometimes an HTTP header or a JWT token must be passed to the retrievers. You can pass a local context object inside the execution input.

final ExecutionInput executionInput = ExecutionInput.newExecutionInput()
    .localContext("aLocalContextObject")
    .query("{add(a: 1, b: 2)}")
    .build();

The localContext object is now accessible from your IntrospectionRetriever and QueryRetriever.

Subscription

GraphQL provides a subscription methodology for providing continuous data. It's a well-known pub/sub pattern for applications. If your application needs a continuous stream of real-time events, notifications, sensor data. Subscription might be wise choice for you.

Use Lilo framework library

Subscription usage needs a few configuration class definition and handlers definition. Lilo core library is framework-agnostic. If you are using a framework like SpringBoot. Please check the lilo documentation for that specific framework. Currently, SpringBoot is supported. Please check Lilo Spring Documentation.

Subscription Usage

If you're going to use Subscription stitching. You need to define an additional retriever other than QueryRetriever and IntrospectionRetriever. As you might guess, it's a SubscriptionRetriever. Besides retrievers, we need to define handlers:

  • SubscriptionGatewayHandler is responsible for handling web socket sessions coming from GraphQL clients to Lilo Stitching Gateway.
  • SubscriptionSourceHandler is responsible for handling websocket sessions going from gateway to remote GraphQL servers.

Since those handler classes are framework-agnostic. A binding should be implemented between framework and handlers.

Additional Resources

You can watch Tame Your Spring Microservices With GraphQL Stitching Using Lilo presentation.

IMAGE ALT TEXT HERE

Lilo's sisters

lilo's People

Contributors

dependabot[bot] avatar firatkucuk avatar murphye avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lilo's Issues

Initialise all schemas

Can we have support to add all schemas rather than for each.

public @NotNull LiloBuilder schemas(final @NotNull Map<String, SchemaSource> schemaSources) {
this.schemaSources = schemaSources;
return this;
}

Namespace concept as in Braid

In braid, there is a namespace concept. This will make possible to define graphql query with same name to be created in different microservices. Similarly, the schema in lilo should also considered while merging the graphql queries from different microservices.
Now a duplicate exception occurs if there are similar named queries in different microservices while merging the schema

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.