Giter VIP home page Giter VIP logo

graphql-kotlin's Introduction

GraphQL Kotlin

Build Status codecov Maven Central Javadocs Awesome Kotlin Badge

Most GraphQL libraries require developers to maintain two sources of truth for their GraphQL API: the schema and the corresponding code (data fetchers or resolvers, and types). Given the similarities between Kotlin and GraphQL, such as the ability to define nullable/non-nullable types, a schema can be generated from Kotlin code without any separate schema specification. graphql-kotlin builds upon graphql-java to allow code-only, or resolver-first, GraphQL services to be built.

For information on GraphQL, please visit the GraphQL website.

For information on graphql-java, please visit GraphQL Java.

Installation

Using a JVM dependency manager, simply link graphql-kotlin to your project.

With Maven:

<dependency>
  <groupId>com.expedia</groupId>
  <artifactId>graphql-kotlin</artifactId>
  <version>${latestVersion}</version>
</dependency>

With Gradle:

compile(group: 'com.expedia', name: 'graphql-kotlin', version: "$latestVersion")

Usage

// Your existing Kotlin code

data class Widget(val id: Int, val value: String)

class WidgetService {
  fun widgetById(id: Int): Widget? {
    // grabs widget from a data source, might return null
  }
  
  @Deprecated("Use widgetById")
  fun widgetByValue(value: String): Widget? {
    // grabs widget from a deprecated data source, might return null
  }
}

class WidgetUpdater {
  fun saveWidget(value: String): Widget {
    // Create and save a new widget, returns non-null
  }
}

// Generate the schema

val config = SchemaGeneratorConfig(supportedPackages = listOf("org.example"))
val queries = listOf(TopLevelObject(WidgetService()))
val mutations = listOf(TopLevelObject(WidgetUpdater()))

toSchema(config, queries, mutations)

will generate

schema {
  query: Query
  mutation: Mutation
}

type Query {
  widgetById(id: Int!): Widget
  
  widgetByValue(vale: String!): Widget @deprecated(reason: "Use widgetById")
}

type Mutation {
  saveWidget(value: String!): Widget!
}

type Widget {
  id: Int!
  value: String!
}

Documentation

There are more examples and documention in our Wiki or you can view the javadocs for all published versions.

If you have a question about something you can not find in our wiki or javadocs, feel free to create an issue and tag it with the question label.

Example

One way to run a GraphQL server is with Spring Boot. A sample Spring Boot app that uses Spring Webflux together with graphql-kotlin and graphql-playground is provided in the example folder. All the examples used in this documentation should be available in the sample app.

In order to run it you can run Application.kt directly from your IDE. Alternatively you can also use the Spring Boot maven plugin by running mvn spring-boot:run from the command line. Once the app has started you can explore the example schema by opening GraphiQL endpoint at http://localhost:8080/playground.

graphql-kotlin's People

Contributors

smyrick avatar dariuszkuc avatar gscheibel avatar brennantaylor avatar rickfast avatar d4rken avatar rharriso avatar sgeb avatar bherrmann2 avatar macarse avatar dennisfen avatar tapaderster avatar bakert 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.