Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-31: DefaultGormDataFetcher does not work for domains with not default datastore #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.grails.gorm.graphql.fetcher
import grails.gorm.DetachedCriteria
import grails.gorm.multitenancy.Tenants
import grails.gorm.transactions.GrailsTransactionTemplate
import grails.gorm.transactions.TransactionService
import graphql.schema.DataFetcher
import graphql.schema.DataFetchingEnvironment
import groovy.transform.CompileStatic
Expand All @@ -17,11 +16,10 @@ import org.grails.datastore.mapping.model.PersistentProperty
import org.grails.datastore.mapping.model.types.Association
import org.grails.datastore.mapping.multitenancy.MultiTenantCapableDatastore
import org.grails.datastore.mapping.transactions.CustomizableRollbackTransactionAttribute
import org.grails.datastore.mapping.transactions.TransactionCapableDatastore
import org.grails.gorm.graphql.entity.EntityFetchOptions
import org.springframework.transaction.PlatformTransactionManager

import java.lang.reflect.Method

/**
* A generic class to assist with querying entities with GraphQL
*
Expand Down Expand Up @@ -111,18 +109,38 @@ abstract class DefaultGormDataFetcher<T> implements DataFetcher<T> {
protected Object withTransaction(boolean readOnly, Closure closure) {
Datastore datastore
if (entity.multiTenant && this.datastore instanceof MultiTenantCapableDatastore) {
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)this.datastore
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore) this.datastore
Serializable currentTenantId = Tenants.currentId(multiTenantCapableDatastore)
datastore = multiTenantCapableDatastore.getDatastoreForTenantId(currentTenantId)
}
else {
} else {
datastore = this.datastore
}

TransactionService txService = datastore.getService(TransactionService)
//To support older versions of GORM
try {
PlatformTransactionManager transactionManager = getTransactionManager(datastore)
CustomizableRollbackTransactionAttribute transactionAttribute = new CustomizableRollbackTransactionAttribute()
transactionAttribute.setReadOnly(readOnly)
new GrailsTransactionTemplate(transactionManager, transactionAttribute).execute(closure)
} catch (NoSuchMethodException | SecurityException e) {
log.error('Unable to find a transaction manager for datastore {}', datastore.class.name)
null
}

//Supports 6.1.x+ only
/*
TransactionService txService = (TransactionService)datastore.getService((Class<?>)TransactionService)
CustomizableRollbackTransactionAttribute transactionAttribute = new CustomizableRollbackTransactionAttribute()
transactionAttribute.setReadOnly(readOnly)
txService.withTransaction(transactionAttribute, closure)
*/
}

private static PlatformTransactionManager getTransactionManager(Datastore datastore) {
if (!datastore instanceof TransactionCapableDatastore) {
throw new IllegalArgumentException("Domain mapped DataStore should be transactional")
}
return ((TransactionCapableDatastore) datastore).getTransactionManager()
}

abstract T get(DataFetchingEnvironment environment)
Expand Down