GithubHelp home page GithubHelp logo

graphql-sandbox's Introduction

GraphQL Sandbox

The intent of this project is to learn how we can cleanly call GraphQL libraries from Java.

Building

It's a Maven project. Simply run mvn clean install

Configuration

GitHub does not allow anonymous access to the V4 API. You can add this in the test.properties file

Source Generation

The apollo-client-maven-plugin is used to build Java objects based on GraphQL schema & query files. For example the source.graphql file contains:

query Source($repoName:String!, $repoOwner:String!, $blob:String!){
     repository(owner: $repoOwner, name: $repoName) {
       content:object(expression: $blob) {
         ... on Blob {
           text
         }
       }
     }
   }

This is a GraphQL query that will fetch the file contents based on the repository, branch, and path. When the Maven plugin runs it will generate objects to build this query in a type safe way. For example:

BlameQuery query = BlameQuery.builder()
  .repoOwner(repoOwner)
  .repoName(repoName)
  .ref(ref)
  .path(path)
  .blob(ref + ":" + path)
  .build();

Note: If you want to play around with GraphQL and learn how to create GraphQL queries you can check out the GitHub Explorer

Creating the Client

These query objects are used with the Apollo Client, which uses the OkHTTP Client.

OkHttpClient okHttpClient = new OkHttpClient.Builder()
  .addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addHeader("Authorization", "Bearer " + token).build()))
  .build();
   
ApolloClient client = ApolloClient.builder()
  .serverUrl(url)
  .okHttpClient(okHttpClient)
  .build();

Executing Query

This client can then be called with the following:

client.query(query).enqueue(new ApolloCall.Callback<Optional<BlameQuery.Data>>() {
    @Override
    public void onResponse(@NotNull Response<Optional<BlameQuery.Data>> response) {
      . . .
    }
    @Override
    public void onFailure(@NotNull ApolloException e) {
      . . .
    }
  }
);

graphql-sandbox's People

Contributors

ccaspanello avatar

Watchers

 avatar  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.