Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
16 changes: 8 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ buildscript {
}
}

plugins {
id 'com.gradle.build-scan' version '2.0.2'
}
//plugins {
// id 'com.gradle.build-scan' version '2.0.2'
//}

repositories {
maven { url "https://repo.grails.org/grails/core" }
Expand All @@ -22,7 +22,7 @@ repositories {
version project.projectVersion

ext {
commonBuild = 'https://raw.githubusercontent.com/grails/grails-common-build/47f0f2e0d9d82030b758c6dfe7bc7eefe6582342'
commonBuild = 'https://raw.githubusercontent.com/grails/grails-common-build/9531dd5e6d774b86e2d8220aff3bcec3add43320'
}

subprojects {
Expand Down Expand Up @@ -93,7 +93,7 @@ subprojects {
}
}

buildScan {
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
}
//buildScan {
// termsOfServiceUrl = 'https://gradle.com/terms-of-service'
// termsOfServiceAgree = 'yes'
//}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.grails.gorm.graphql.entity.operations.arguments
package org.grails.gorm.graphql.entity.arguments

import graphql.schema.GraphQLInputType
import groovy.transform.CompileStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.grails.gorm.graphql.entity.operations.arguments
package org.grails.gorm.graphql.entity.arguments

import graphql.schema.GraphQLArgument
import graphql.schema.GraphQLInputType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.grails.gorm.graphql.entity.operations.arguments
package org.grails.gorm.graphql.entity.arguments

import graphql.schema.GraphQLInputType
import groovy.transform.CompileStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.grails.gorm.graphql.entity.dsl.helpers

import graphql.schema.GraphQLArgument
import groovy.transform.CompileStatic
import org.grails.datastore.mapping.model.MappingContext
import org.grails.gorm.graphql.entity.arguments.ComplexArgument
import org.grails.gorm.graphql.entity.arguments.CustomArgument
import org.grails.gorm.graphql.entity.arguments.SimpleArgument
import org.grails.gorm.graphql.types.GraphQLTypeManager

/**
* Decorates a class with a description property and builder method.
*
* @param <T> The implementing class
* @author James Kleeh
* @since 1.0.0
*/
@CompileStatic
trait Arguable<T> extends ExecutesClosures {

List<CustomArgument> arguments = []

private void handleArgumentClosure(CustomArgument argument, Closure closure) {
withDelegate(closure, (Object)argument)
argument.validate()
arguments.add(argument)
}

List<GraphQLArgument> getArguments(GraphQLTypeManager typeManager, MappingContext mappingContext) {
arguments.collect {
it.getArgument(typeManager, mappingContext).build()
}
}

/**
* Creates an argument to the operation that is a list of a simple type.
* The list can not have more than 1 element and that element must be a class.
*
* @param name The name of the argument
* @param type The returnType of the argument
* @param closure To provide additional data about the argument
* @return The operation in order to chain method calls
*/
T argument(String name, List<Class<?>> type, @DelegatesTo(value = SimpleArgument, strategy = Closure.DELEGATE_ONLY) Closure closure = null) {
CustomArgument argument = new SimpleArgument().name(name).returns(type)
handleArgumentClosure(argument, closure)
(T)this
}

/**
* Creates an argument to the operation that is of the returnType provided.
*
* @param name The name of the argument
* @param type The returnType of the argument
* @param closure To provide additional data about the argument
* @return The operation in order to chain method calls
*/
T argument(String name, Class<?> type, @DelegatesTo(value = SimpleArgument, strategy = Closure.DELEGATE_ONLY) Closure closure = null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar, in that when the implementing GORM entity used @GrailsCompileStatic, this would result in a static compilation error without the generic wildcard.

CustomArgument argument = new SimpleArgument().name(name).returns(type)
handleArgumentClosure(argument, closure)
(T)this
}

/**
* Creates an argument to the operation that is a custom type.
*
* @param name The name of the argument
* @param closure To provide additional data about the argument
* @return The operation in order to chain method calls
*/
T argument(String name, String typeName, @DelegatesTo(value = ComplexArgument, strategy = Closure.DELEGATE_ONLY) Closure closure) {
CustomArgument argument = new ComplexArgument().name(name).typeName(typeName)
handleArgumentClosure(argument, closure)
(T)this
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package org.grails.gorm.graphql.entity.dsl.helpers

import graphql.schema.GraphQLInputObjectType
import graphql.schema.GraphQLInputType
import graphql.schema.GraphQLList
import graphql.schema.GraphQLNonNull
import graphql.schema.GraphQLOutputType
import graphql.schema.GraphQLType
import graphql.schema.*
import groovy.transform.CompileStatic
import graphql.schema.GraphQLObjectType
import org.grails.datastore.mapping.model.MappingContext
import org.grails.gorm.graphql.entity.fields.ComplexField
import org.grails.gorm.graphql.entity.fields.Field
Expand All @@ -25,7 +19,7 @@ import static graphql.schema.GraphQLInputObjectField.newInputObjectField
* @since 1.0.0
*/
@CompileStatic
trait ComplexTyped<T> {
trait ComplexTyped<T> extends ExecutesClosures {

boolean collection = false

Expand All @@ -43,24 +37,6 @@ trait ComplexTyped<T> {
(T)this
}

/**
* This method exists because of https://issues.apache.org/jira/browse/GROOVY-8272
*
* Normally this class could extend from {@link ExecutesClosures}
*/
private void withDelegate(Closure closure, Object delegate) {
if (closure != null) {
closure.resolveStrategy = Closure.DELEGATE_ONLY
closure.delegate = delegate

try {
closure.call()
} finally {
closure.delegate = null
}
}
}

/**
* Builds a custom object returnType if the supplied return returnType is a Map
*
Expand Down Expand Up @@ -131,7 +107,7 @@ trait ComplexTyped<T> {

private void handleField(Closure closure, Field field) {
field.nullable(defaultNull)
withDelegate(closure, field)
withDelegate(closure, (Object)field)
handleField(field)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import groovy.transform.CompileStatic
import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.gorm.graphql.GraphQLServiceManager
import org.grails.gorm.graphql.entity.dsl.helpers.Arguable
import org.grails.gorm.graphql.entity.dsl.helpers.Deprecatable
import org.grails.gorm.graphql.entity.dsl.helpers.Describable
import org.grails.gorm.graphql.entity.dsl.helpers.ExecutesClosures
import org.grails.gorm.graphql.entity.dsl.helpers.Named
import org.grails.gorm.graphql.entity.operations.arguments.ComplexArgument
import org.grails.gorm.graphql.entity.operations.arguments.CustomArgument
import org.grails.gorm.graphql.entity.operations.arguments.SimpleArgument
import org.grails.gorm.graphql.entity.arguments.CustomArgument
import org.grails.gorm.graphql.fetcher.interceptor.CustomMutationInterceptorInvoker
import org.grails.gorm.graphql.fetcher.interceptor.CustomQueryInterceptorInvoker
import org.grails.gorm.graphql.fetcher.interceptor.InterceptingDataFetcher
Expand All @@ -29,69 +28,20 @@ import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition
* @since 1.0.0
*/
@CompileStatic
abstract class CustomOperation<T> implements Named<T>, Describable<T>, Deprecatable<T>, ExecutesClosures {
abstract class CustomOperation<T> implements Named<T>, Describable<T>, Deprecatable<T>, Arguable<T>, ExecutesClosures {

private List<CustomArgument> arguments = []
private static InterceptorInvoker queryInvoker = new CustomQueryInterceptorInvoker()
private static InterceptorInvoker mutationInvoker = new CustomMutationInterceptorInvoker()
DataFetcher dataFetcher
boolean defaultListArguments = false

T dataFetcher(DataFetcher dataFetcher) {
T dataFetcher(DataFetcher<?> dataFetcher) {
this.dataFetcher = dataFetcher
(T)this
}

OperationType operationType

private void handleArgumentClosure(CustomArgument argument, Closure closure) {
withDelegate(closure, argument)
argument.validate()
arguments.add(argument)
}

/**
* Creates an argument to the operation that is a list of a simple type.
* The list can not have more than 1 element and that element must be a class.
*
* @param name The name of the argument
* @param type The returnType of the argument
* @param closure To provide additional data about the argument
* @return The operation in order to chain method calls
*/
CustomOperation argument(String name, List<Class> type, @DelegatesTo(value = SimpleArgument, strategy = Closure.DELEGATE_ONLY) Closure closure = null) {
CustomArgument argument = new SimpleArgument().name(name).returns(type)
handleArgumentClosure(argument, closure)
this
}

/**
* Creates an argument to the operation that is of the returnType provided.
*
* @param name The name of the argument
* @param type The returnType of the argument
* @param closure To provide additional data about the argument
* @return The operation in order to chain method calls
*/
CustomOperation argument(String name, Class type, @DelegatesTo(value = SimpleArgument, strategy = Closure.DELEGATE_ONLY) Closure closure = null) {
CustomArgument argument = new SimpleArgument().name(name).returns(type)
handleArgumentClosure(argument, closure)
this
}

/**
* Creates an argument to the operation that is a custom type.
*
* @param name The name of the argument
* @param closure To provide additional data about the argument
* @return The operation in order to chain method calls
*/
CustomOperation argument(String name, String typeName, @DelegatesTo(value = ComplexArgument, strategy = Closure.DELEGATE_ONLY) Closure closure) {
CustomArgument argument = new ComplexArgument().name(name).typeName(typeName)
handleArgumentClosure(argument, closure)
this
}

/**
* If the argument is true, the default list arguments created in the
* schema through configuration will be prepended to any other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import graphql.schema.GraphQLType
import groovy.transform.AutoClone
import groovy.transform.CompileStatic
import org.grails.datastore.mapping.model.MappingContext
import org.grails.gorm.graphql.entity.dsl.helpers.Arguable
import org.grails.gorm.graphql.entity.dsl.helpers.Deprecatable
import org.grails.gorm.graphql.entity.dsl.helpers.Describable
import org.grails.gorm.graphql.entity.dsl.helpers.Named
Expand All @@ -23,7 +24,7 @@ import org.grails.gorm.graphql.types.GraphQLTypeManager
*/
@AutoClone
@CompileStatic
abstract class CustomGraphQLProperty<T> extends OrderedGraphQLProperty implements Named<T>, Describable<T>, Deprecatable<T>, Nullable<T> {
abstract class CustomGraphQLProperty<T> extends OrderedGraphQLProperty implements Named<T>, Describable<T>, Deprecatable<T>, Nullable<T>, Arguable<T> {

Integer order = null
boolean input = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MockDataFetchingEnvironment implements DataFetchingEnvironment {

@Override
Object getArgumentOrDefault(String name, Object defaultValue) {
return arguments.getOrDefault(name, defaultValue)
arguments.getOrDefault(name, defaultValue)
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package org.grails.gorm.graphql.types.output

import graphql.TypeResolutionEnvironment
import graphql.schema.GraphQLCodeRegistry
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLInterfaceType
import graphql.schema.GraphQLObjectType
import graphql.schema.GraphQLOutputType
import graphql.schema.TypeResolver
import graphql.schema.*
import groovy.transform.CompileStatic
import org.grails.datastore.mapping.model.MappingContext
import org.grails.datastore.mapping.model.PersistentEntity
import org.grails.gorm.graphql.GraphQLEntityHelper
import org.grails.gorm.graphql.entity.dsl.helpers.Arguable
import org.grails.gorm.graphql.entity.property.GraphQLDomainProperty
import org.grails.gorm.graphql.entity.property.manager.GraphQLDomainPropertyManager
import org.grails.gorm.graphql.response.errors.GraphQLErrorsResponseHandler
Expand Down Expand Up @@ -68,6 +65,16 @@ abstract class AbstractObjectTypeBuilder implements ObjectTypeBuilder {
field
}

protected GraphQLFieldDefinition.Builder addFieldArgs(GraphQLFieldDefinition.Builder field, GraphQLDomainProperty prop, MappingContext mapping) {
if (prop instanceof Arguable) {
List<GraphQLArgument> arguments = prop.getArguments(typeManager, mapping)
if (!arguments.isEmpty()) {
field.arguments(arguments)
}
}
field
}

@Override
GraphQLOutputType build(PersistentEntity entity) {

Expand All @@ -85,7 +92,9 @@ abstract class AbstractObjectTypeBuilder implements ObjectTypeBuilder {
List<GraphQLDomainProperty> properties = builder.getProperties(entity)
for (GraphQLDomainProperty prop: properties) {
if (prop.output) {
fields.add(buildField(prop, NAME).build())
GraphQLFieldDefinition.Builder field = buildField(prop, NAME)
addFieldArgs(field, prop, entity.mappingContext)
fields.add(field.build())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ class CustomType {
}
description 'This is a description'
}
add('withArgument', 'WithArg') {
argument('arg', String)
type {
field('response', String)
}
}
add('withArgumentList', 'WithArgList') {
argument('arg', [String])
type {
field('response', String)
}
}
add('withCustomArgument', 'WithCustomArg') {
argument('arg', 'CustomArg') {
accepts {
field('field', String)
}
}
type {
field('response', String)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ class SimpleType {
}

add('list', [String])

add('withArgument', String) {
argument('arg', String)
}
add('withArgumentList', String) {
argument('arg', [String])
}
add('withCustomArgument', String) {
argument('arg', 'SimpleArg') {
accepts {
field('field', String)
}
}
}
}

def methodMissing(String name, Object[] args) {
Expand Down
Loading