GithubHelp home page GithubHelp logo

isabella232 / federation-jvm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apollographql/federation-jvm

0.0 0.0 0.0 509 KB

JVM support for Apollo federation

Home Page: https://www.apollographql.com/docs/apollo-server/federation/introduction/

License: MIT License

Java 100.00%

federation-jvm's Introduction

MIT License Maven Central CircleCI

Apollo Federation on the JVM

Packages published to Maven Central; release notes in RELEASE_NOTES.md. Note that older versions of this package may only be available in JCenter, but we are planning to republish these versions to Maven Central.

An example of graphql-spring-boot microservice is available in spring-example.

Getting started

Dependency management with Gradle

Make sure Maven Central is among your repositories:

repositories {
    mavenCentral()
}

Add a dependency to graphql-java-support:

dependencies {
    implementation 'com.apollographql.federation:federation-graphql-java-support:0.9.0'
}

graphql-java schema transformation

graphql-java-support produces a graphql.schema.GraphQLSchema by transforming your existing schema in accordance to the federation specification. It follows the Builder pattern.

Start with com.apollographql.federation.graphqljava.Federation.transform(…), which can receive either:

  • A GraphQLSchema;
  • A TypeDefinitionRegistry, optionally with a RuntimeWiring;
  • A String, Reader, or File declaring the schema using the Schema Definition Language, optionally with a RuntimeWiring;

and returns a SchemaTransformer.

If your schema does not contain any types annotated with the @key directive, nothing else is required. You can build a transformed GraphQLSchema with SchemaTransformer#build(), and confirm it exposes query { _schema { sdl } }.

Otherwise, all types annotated with @key will be part of the _Entity union type, and reachable through query { _entities(representations: [Any!]!) { … } }. Before calling SchemaTransformer#build(), you will also need to provide:

  • A TypeResolver for _Entity using SchemaTransformer#resolveEntityType(TypeResolver);
  • A DataFetcher or DataFetcherFactory for _entities using SchemaTransformer#fetchEntities(DataFetcher|DataFetcherFactory).

A minimal but complete example is available in AppConfiguration.

Federated tracing

To make your server generate performance traces and return them along with responses to the Apollo Gateway (which then can send them to Apollo Graph Manager), install the FederatedTracingInstrumentation into your GraphQL object:

GraphQL graphql = GraphQL.newGraphQL(graphQLSchema)
        .instrumentation(new FederatedTracingInstrumentation())
        .build();

It is generally desired to only create traces for requests that actually come from Apollo Gateway, as they aren't helpful if you're connecting directly to your backend service for testing. In order for FederatedTracingInstrumentation to know if the request is coming from Gateway, you should populate the tracing header information directly in the GraphQLContext map.

Map<Object, Object> contextMap = new HashMap<>();
String federatedTracingHeaderValue = httpRequest.getHeader(FEDERATED_TRACING_HEADER_NAME);
if (federatedTracingHeaderValue != null) {
    contextMap.put(FEDERATED_TRACING_HEADER_NAME, federatedTracingHeaderValue);
}

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
        .graphQLContext(contextMap)
        .query(queryString)
        .build();
graphql.executeAsync(executionInput);

federation-jvm's People

Contributors

sachindshinde avatar pcarrier avatar glasser avatar martinbonnin avatar thejc avatar tinnou avatar lennyburdette avatar setchy avatar lanceon avatar caydie-tran avatar jsegaran avatar mcohen75 avatar rkudryashov avatar dependabot[bot] avatar kenshih avatar

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.