feat : Schema, Query, Mutation, Subscription 추가

This commit is contained in:
banjjoknim
2022-08-02 14:12:35 +09:00
parent 4516309e8f
commit 5bb45cc2e6
7 changed files with 105 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package com.banjjoknim.graphqlkotlin.person
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import com.expediagroup.graphql.server.Schema
import org.springframework.stereotype.Component
/**
* In order to expose your schema directives, queries, mutations, and subscriptions in the GraphQL schema create beans that implement the corresponding marker interface and they will be automatically picked up by graphql-kotlin-spring-server auto-configuration library.
*/
@GraphQLDescription("Sample GraphQL Schema")
@Component
class GraphQLSchema : Schema

View File

@@ -0,0 +1,5 @@
package com.banjjoknim.graphqlkotlin.person
data class Person(
var name: String,
)

View File

@@ -0,0 +1,14 @@
package com.banjjoknim.graphqlkotlin.person
import com.expediagroup.graphql.server.operations.Mutation
import org.springframework.stereotype.Component
@Component
class PersonMutation : Mutation {
fun changeName(person: Person, newName: String): Person {
return person.apply {
name = newName
}
}
}

View File

@@ -0,0 +1,37 @@
package com.banjjoknim.graphqlkotlin.person
import com.expediagroup.graphql.generator.annotations.GraphQLIgnore
import com.expediagroup.graphql.server.operations.Query
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
@Component
class PersonQuery(
/**
* # Spring Beans
*
* Since the top level objects are Spring components, Spring will automatically autowire dependent beans as normal.
*
* Refer to [Spring Documentation](https://docs.spring.io/spring-framework/docs/current/reference/html/) for details.
*/
private val personRepository: PersonRepository
) : Query {
fun getPerson(name: String): Person = Person(name)
/**
* # Spring Beans in Arguments
*
* graphql-kotlin-spring-server provides Spring-aware data fetcher that automatically autowires Spring beans when they are specified as function arguments.
*
* `@Autowired` arguments should be explicitly excluded from the GraphQL schema by also specifying @GraphQLIgnore.
*
* ```
* NOTE
* If you are using custom data fetcher make sure that you extend SpringDataFetcher instead of the base FunctionDataFetcher to keep this functionallity.
* ```
*/
fun findPerson(@GraphQLIgnore @Autowired personRepository: PersonRepository, name: String): Person? {
return personRepository.findPerson(name)
}
}

View File

@@ -0,0 +1,6 @@
package com.banjjoknim.graphqlkotlin.person
interface PersonRepository {
fun findPerson(name: String): Person?
}

View File

@@ -0,0 +1,18 @@
package com.banjjoknim.graphqlkotlin.person
import org.springframework.stereotype.Repository
@Repository
class PersonRepositoryImpl : PersonRepository {
companion object {
private val people = mapOf(
"banjjoknim" to Person("banjjoknim"),
"colt" to Person("colt")
)
}
override fun findPerson(name: String): Person? {
return people[name]
}
}

View File

@@ -0,0 +1,13 @@
package com.banjjoknim.graphqlkotlin.person
import com.expediagroup.graphql.server.operations.Subscription
import org.reactivestreams.Publisher
import org.springframework.stereotype.Component
@Component
class PersonSubscription : Subscription {
fun changeName(person: Person, newName: String): Publisher<Person> {
return Publisher { println("change name published") }
}
}