Search in sources :

Example 1 with DataLoader

use of org.dataloader.DataLoader in project graphql-java by graphql-java.

the class HttpMain method buildDataLoaderRegistry.

private DataLoaderRegistry buildDataLoaderRegistry() {
    BatchLoader<String, Object> friendsBatchLoader = keys -> CompletableFuture.supplyAsync(() -> loadCharactersViaHTTP(keys));
    DataLoader<String, Object> friendsDataLoader = new DataLoader<>(friendsBatchLoader);
    DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
    // 
    // we make sure our dataloader is in the registry
    dataLoaderRegistry.register("friends", friendsDataLoader);
    return dataLoaderRegistry;
}
Also used : Request(org.eclipse.jetty.server.Request) ExecutionInput.newExecutionInput(graphql.ExecutionInput.newExecutionInput) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) GraphQL(graphql.GraphQL) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecutionResult(graphql.ExecutionResult) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) HttpServletRequest(javax.servlet.http.HttpServletRequest) BatchLoader(org.dataloader.BatchLoader) DataLoaderDispatcherInstrumentationOptions.newOptions(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationOptions.newOptions) SchemaParser(graphql.schema.idl.SchemaParser) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) TracingInstrumentation(graphql.execution.instrumentation.tracing.TracingInstrumentation) TypeResolver(graphql.schema.TypeResolver) Server(org.eclipse.jetty.server.Server) StarWarsData(graphql.StarWarsData) GraphQLObjectType(graphql.schema.GraphQLObjectType) HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletResponse(javax.servlet.http.HttpServletResponse) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) IOException(java.io.IOException) DataLoader(org.dataloader.DataLoader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) TypeRuntimeWiring.newTypeWiring(graphql.schema.idl.TypeRuntimeWiring.newTypeWiring) ExecutionInput(graphql.ExecutionInput) ChainedInstrumentation(graphql.execution.instrumentation.ChainedInstrumentation) List(java.util.List) Instrumentation(graphql.execution.instrumentation.Instrumentation) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) InputStream(java.io.InputStream) DataLoader(org.dataloader.DataLoader) DataLoaderRegistry(org.dataloader.DataLoaderRegistry)

Example 2 with DataLoader

use of org.dataloader.DataLoader in project graphql-java by graphql-java.

the class HttpMain method buildStarWarsSchema.

private GraphQLSchema buildStarWarsSchema() {
    // 
    if (starWarsSchema == null) {
        // 
        // 
        // the fetcher of friends uses java-dataloader to make the circular friends fetching
        // more efficient by batching and caching the calls to load Character friends
        // 
        DataFetcher friendsFetcher = environment -> {
            DataLoaderRegistry dataloaderRegistry = asMapGet(environment.getContext(), "dataloaderRegistry");
            DataLoader friendsDataLoader = dataloaderRegistry.getDataLoader("friends");
            List<String> friendIds = asMapGet(environment.getSource(), "friends");
            return friendsDataLoader.loadMany(friendIds);
        };
        // 
        // reads a file that provides the schema types
        // 
        Reader streamReader = loadSchemaFile("starWarsSchemaAnnotated.graphqls");
        TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(streamReader);
        // 
        // the runtime wiring is used to provide the code that backs the
        // logical schema
        // 
        TypeResolver characterTypeResolver = env -> {
            Map<String, Object> obj = (Map<String, Object>) env.getObject();
            String id = (String) obj.get("id");
            GraphQLSchema schema = env.getSchema();
            if (StarWarsData.isHuman(id)) {
                return (GraphQLObjectType) schema.getType("Human");
            } else {
                return (GraphQLObjectType) schema.getType("Droid");
            }
        };
        RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring().type(newTypeWiring("Query").dataFetcher("hero", StarWarsData.getHeroDataFetcher()).dataFetcher("human", StarWarsData.getHumanDataFetcher()).dataFetcher("droid", StarWarsData.getDroidDataFetcher())).type(newTypeWiring("Human").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Droid").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Character").typeResolver(characterTypeResolver)).type(newTypeWiring("Episode").enumValues(StarWarsData.getEpisodeResolver())).build();
        // finally combine the logical schema with the physical runtime
        starWarsSchema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
    }
    return starWarsSchema;
}
Also used : Request(org.eclipse.jetty.server.Request) ExecutionInput.newExecutionInput(graphql.ExecutionInput.newExecutionInput) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) GraphQL(graphql.GraphQL) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecutionResult(graphql.ExecutionResult) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) HttpServletRequest(javax.servlet.http.HttpServletRequest) BatchLoader(org.dataloader.BatchLoader) DataLoaderDispatcherInstrumentationOptions.newOptions(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationOptions.newOptions) SchemaParser(graphql.schema.idl.SchemaParser) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) DataFetcher(graphql.schema.DataFetcher) GraphQLSchema(graphql.schema.GraphQLSchema) TracingInstrumentation(graphql.execution.instrumentation.tracing.TracingInstrumentation) TypeResolver(graphql.schema.TypeResolver) Server(org.eclipse.jetty.server.Server) StarWarsData(graphql.StarWarsData) GraphQLObjectType(graphql.schema.GraphQLObjectType) HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletResponse(javax.servlet.http.HttpServletResponse) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) IOException(java.io.IOException) DataLoader(org.dataloader.DataLoader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) TypeRuntimeWiring.newTypeWiring(graphql.schema.idl.TypeRuntimeWiring.newTypeWiring) ExecutionInput(graphql.ExecutionInput) ChainedInstrumentation(graphql.execution.instrumentation.ChainedInstrumentation) List(java.util.List) Instrumentation(graphql.execution.instrumentation.Instrumentation) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) InputStream(java.io.InputStream) TypeResolver(graphql.schema.TypeResolver) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) SchemaGenerator(graphql.schema.idl.SchemaGenerator) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) DataLoader(org.dataloader.DataLoader) RuntimeWiring(graphql.schema.idl.RuntimeWiring) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) HandlerList(org.eclipse.jetty.server.handler.HandlerList) List(java.util.List) DataFetcher(graphql.schema.DataFetcher) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DataLoader

use of org.dataloader.DataLoader in project graphql-java by graphql-java.

the class BatchingExamples method starWarsExample.

void starWarsExample() {
    // a batch loader function that will be called with N or more keys for batch loading
    BatchLoader<String, Object> characterBatchLoader = new BatchLoader<String, Object>() {

        @Override
        public CompletionStage<List<Object>> load(List<String> keys) {
            // 
            return CompletableFuture.supplyAsync(() -> getCharacterDataViaBatchHTTPApi(keys));
        }
    };
    // a data loader for characters that points to the character batch loader
    DataLoader<String, Object> characterDataLoader = new DataLoader<>(characterBatchLoader);
    // 
    // use this data loader in the data fetchers associated with characters and put them into
    // the graphql schema (not shown)
    // 
    DataFetcher heroDataFetcher = new DataFetcher() {

        @Override
        public Object get(DataFetchingEnvironment environment) {
            // R2D2
            return characterDataLoader.load("2001");
        }
    };
    DataFetcher friendsDataFetcher = new DataFetcher() {

        @Override
        public Object get(DataFetchingEnvironment environment) {
            StarWarsCharacter starWarsCharacter = environment.getSource();
            List<String> friendIds = starWarsCharacter.getFriendIds();
            return characterDataLoader.loadMany(friendIds);
        }
    };
    // 
    // DataLoaderRegistry is a place to register all data loaders in that needs to be dispatched together
    // in this case there is 1 but you can have many
    // 
    DataLoaderRegistry registry = new DataLoaderRegistry();
    registry.register("character", characterDataLoader);
    // 
    // this instrumentation implementation will dispatched all the dataloaders
    // as each level fo the graphql query is executed and hence make batched objects
    // available to the query and the associated DataFetchers
    // 
    DataLoaderDispatcherInstrumentation dispatcherInstrumentation = new DataLoaderDispatcherInstrumentation(registry);
    // 
    // now build your graphql object and execute queries on it.
    // the data loader will be invoked via the data fetchers on the
    // schema fields
    // 
    GraphQL graphQL = GraphQL.newGraphQL(buildSchema()).instrumentation(dispatcherInstrumentation).build();
}
Also used : GraphQL(graphql.GraphQL) DataLoaderDispatcherInstrumentation(graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) BatchLoader(org.dataloader.BatchLoader) DataLoader(org.dataloader.DataLoader) DataLoaderRegistry(org.dataloader.DataLoaderRegistry) List(java.util.List) DataFetcher(graphql.schema.DataFetcher)

Aggregations

GraphQL (graphql.GraphQL)3 DataLoaderDispatcherInstrumentation (graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentation)3 DataFetcher (graphql.schema.DataFetcher)3 List (java.util.List)3 BatchLoader (org.dataloader.BatchLoader)3 DataLoader (org.dataloader.DataLoader)3 DataLoaderRegistry (org.dataloader.DataLoaderRegistry)3 ExecutionInput (graphql.ExecutionInput)2 ExecutionInput.newExecutionInput (graphql.ExecutionInput.newExecutionInput)2 ExecutionResult (graphql.ExecutionResult)2 StarWarsData (graphql.StarWarsData)2 ChainedInstrumentation (graphql.execution.instrumentation.ChainedInstrumentation)2 Instrumentation (graphql.execution.instrumentation.Instrumentation)2 DataLoaderDispatcherInstrumentationOptions.newOptions (graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationOptions.newOptions)2 TracingInstrumentation (graphql.execution.instrumentation.tracing.TracingInstrumentation)2 GraphQLObjectType (graphql.schema.GraphQLObjectType)2 GraphQLSchema (graphql.schema.GraphQLSchema)2 TypeResolver (graphql.schema.TypeResolver)2 RuntimeWiring (graphql.schema.idl.RuntimeWiring)2 SchemaGenerator (graphql.schema.idl.SchemaGenerator)2