diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Typed.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Typed.groovy index 1b892b83..793761f4 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Typed.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Typed.groovy @@ -53,7 +53,7 @@ trait Typed { graphQLType = typeManager.getEnumType(type, nullable) } else if (typeManager.hasType(type)) { - graphQLType = (GraphQLInputType)typeManager.getType(type, nullable) + graphQLType = typeManager.getType(type, nullable) } else { PersistentEntity entity = mappingContext?.getPersistentEntity(type.name) diff --git a/examples/grails-test-app/grails-app/domain/grails/test/app/Artist.groovy b/examples/grails-test-app/grails-app/domain/grails/test/app/Artist.groovy new file mode 100644 index 00000000..607240a7 --- /dev/null +++ b/examples/grails-test-app/grails-app/domain/grails/test/app/Artist.groovy @@ -0,0 +1,17 @@ +package grails.test.app + +import grails.test.app.pogo.Painting +import org.grails.gorm.graphql.entity.dsl.GraphQLMapping + +class Artist { + + String name + + static graphql = GraphQLMapping.build { + add('paintings', [Painting]) { + dataFetcher { + return [new Painting(name: 'test', artistName: 'Picasso', heightCm: 60, widthCm: 120)] + } + } + } +} diff --git a/examples/grails-test-app/src/integration-test/groovy/grails/test/app/ArtistIntegrationSpec.groovy b/examples/grails-test-app/src/integration-test/groovy/grails/test/app/ArtistIntegrationSpec.groovy new file mode 100644 index 00000000..abd2dfa0 --- /dev/null +++ b/examples/grails-test-app/src/integration-test/groovy/grails/test/app/ArtistIntegrationSpec.groovy @@ -0,0 +1,50 @@ +package grails.test.app + +import grails.gorm.transactions.Rollback +import grails.testing.mixin.integration.Integration +import org.grails.gorm.graphql.plugin.testing.GraphQLSpec +import org.hibernate.SessionFactory +import spock.lang.Specification + +@Integration +@Rollback +class ArtistIntegrationSpec extends Specification implements GraphQLSpec { + + SessionFactory sessionFactory + + void "test listing artists and paintings"() { + given: + def a = new Artist(name: "Picasso").save(flush: true, failOnError: true) + sessionFactory.currentSession.flush() + sessionFactory.currentSession.transaction.commit() + + when: + def resp = graphQL.graphql(""" + { + artistList { + id + name + paintings { + name + heightCm + widthCm + } + } + } + """) + def json = resp.body() + println json.toString() + def artists = json.data.artistList + def artist = artists[0] + + then: + artists.size() == 1 + artist.id == a.id + artist.name == "Picasso" + artist.paintings.size() == 1 + artist.paintings[0].name == "test" + artist.paintings[0].heightCm == 60 + artist.paintings[0].widthCm == 120 + } + +} diff --git a/examples/grails-test-app/src/main/groovy/grails/test/app/GraphQLCustomizer.groovy b/examples/grails-test-app/src/main/groovy/grails/test/app/GraphQLCustomizer.groovy index 0023434a..aff8fa4e 100644 --- a/examples/grails-test-app/src/main/groovy/grails/test/app/GraphQLCustomizer.groovy +++ b/examples/grails-test-app/src/main/groovy/grails/test/app/GraphQLCustomizer.groovy @@ -1,5 +1,9 @@ package grails.test.app +import grails.test.app.pogo.Painting +import grails.test.app.pogo.Profile +import graphql.schema.GraphQLObjectType +import graphql.schema.GraphQLOutputType import org.grails.gorm.graphql.binding.manager.GraphQLDataBinderManager import org.grails.gorm.graphql.fetcher.GraphQLDataFetcherType import org.grails.gorm.graphql.interceptor.impl.BaseGraphQLFetcherInterceptor @@ -14,6 +18,9 @@ import org.grails.gorm.graphql.fetcher.impl.EntityDataFetcher import org.grails.gorm.graphql.fetcher.impl.SingleEntityDataFetcher import org.grails.gorm.graphql.fetcher.impl.SoftDeleteEntityDataFetcher import org.grails.gorm.graphql.fetcher.manager.GraphQLDataFetcherManager +import org.grails.gorm.graphql.types.GraphQLTypeManager + +import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition @CompileStatic @@ -57,4 +64,27 @@ class GraphQLCustomizer extends GraphQLPostProcessor { binderManager.registerDataBinder(User, new UserDataBinder()) binderManager.registerDataBinder(Role, new RoleDataBinder()) } + + @Override + void doWith(GraphQLTypeManager typeManager) { + GraphQLOutputType stringType = (GraphQLOutputType)typeManager.getType(String) + GraphQLOutputType intType = (GraphQLOutputType)typeManager.getType(Integer) + GraphQLObjectType.Builder builder = GraphQLObjectType.newObject() + .name('Painting') + .field(newFieldDefinition() + .name('name') + .type(stringType)) + .field(newFieldDefinition() + .name('artistName') + .type(stringType)) + .field(newFieldDefinition() + .name('heightCm') + .type(intType)) + .field(newFieldDefinition() + .name('widthCm') + .type(intType)) + + + typeManager.registerType(Painting, builder.build()) + } } diff --git a/examples/grails-test-app/src/main/groovy/grails/test/app/pogo/Painting.groovy b/examples/grails-test-app/src/main/groovy/grails/test/app/pogo/Painting.groovy new file mode 100644 index 00000000..4370ffea --- /dev/null +++ b/examples/grails-test-app/src/main/groovy/grails/test/app/pogo/Painting.groovy @@ -0,0 +1,10 @@ +package grails.test.app.pogo + +class Painting { + + String name + String artistName + Integer heightCm + Integer widthCm + +}