diff --git a/build.gradle b/build.gradle index 49fa417b..ab9cea3a 100644 --- a/build.gradle +++ b/build.gradle @@ -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" } @@ -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 { @@ -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' +//} diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/ComplexArgument.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/ComplexArgument.groovy similarity index 96% rename from core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/ComplexArgument.groovy rename to core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/ComplexArgument.groovy index cba70951..7837b889 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/ComplexArgument.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/ComplexArgument.groovy @@ -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 diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/CustomArgument.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/CustomArgument.groovy similarity index 95% rename from core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/CustomArgument.groovy rename to core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/CustomArgument.groovy index de8d4bca..dd8f92db 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/CustomArgument.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/CustomArgument.groovy @@ -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 diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/SimpleArgument.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/SimpleArgument.groovy similarity index 95% rename from core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/SimpleArgument.groovy rename to core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/SimpleArgument.groovy index 7a9a28f4..27c3b2aa 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/arguments/SimpleArgument.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/arguments/SimpleArgument.groovy @@ -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 diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Arguable.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Arguable.groovy new file mode 100644 index 00000000..c3d4a7ef --- /dev/null +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Arguable.groovy @@ -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 The implementing class + * @author James Kleeh + * @since 1.0.0 + */ +@CompileStatic +trait Arguable extends ExecutesClosures { + + List arguments = [] + + private void handleArgumentClosure(CustomArgument argument, Closure closure) { + withDelegate(closure, (Object)argument) + argument.validate() + arguments.add(argument) + } + + List 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> 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) { + 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 + } + +} diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/ComplexTyped.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/ComplexTyped.groovy index 3f9d573c..638b6ac5 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/ComplexTyped.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/ComplexTyped.groovy @@ -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 @@ -25,7 +19,7 @@ import static graphql.schema.GraphQLInputObjectField.newInputObjectField * @since 1.0.0 */ @CompileStatic -trait ComplexTyped { +trait ComplexTyped extends ExecutesClosures { boolean collection = false @@ -43,24 +37,6 @@ trait ComplexTyped { (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 * @@ -131,7 +107,7 @@ trait ComplexTyped { private void handleField(Closure closure, Field field) { field.nullable(defaultNull) - withDelegate(closure, field) + withDelegate(closure, (Object)field) handleField(field) } diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/CustomOperation.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/CustomOperation.groovy index d48b16a6..18e0d7c4 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/CustomOperation.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/operations/CustomOperation.groovy @@ -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 @@ -29,69 +28,20 @@ import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition * @since 1.0.0 */ @CompileStatic -abstract class CustomOperation implements Named, Describable, Deprecatable, ExecutesClosures { +abstract class CustomOperation implements Named, Describable, Deprecatable, Arguable, ExecutesClosures { - private List 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 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 diff --git a/core/src/main/groovy/org/grails/gorm/graphql/entity/property/impl/CustomGraphQLProperty.groovy b/core/src/main/groovy/org/grails/gorm/graphql/entity/property/impl/CustomGraphQLProperty.groovy index dfc17cbf..6420f5a2 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/entity/property/impl/CustomGraphQLProperty.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/entity/property/impl/CustomGraphQLProperty.groovy @@ -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 @@ -23,7 +24,7 @@ import org.grails.gorm.graphql.types.GraphQLTypeManager */ @AutoClone @CompileStatic -abstract class CustomGraphQLProperty extends OrderedGraphQLProperty implements Named, Describable, Deprecatable, Nullable { +abstract class CustomGraphQLProperty extends OrderedGraphQLProperty implements Named, Describable, Deprecatable, Nullable, Arguable { Integer order = null boolean input = true diff --git a/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy b/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy index c34567ac..85f3daff 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy @@ -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 diff --git a/core/src/main/groovy/org/grails/gorm/graphql/types/output/AbstractObjectTypeBuilder.groovy b/core/src/main/groovy/org/grails/gorm/graphql/types/output/AbstractObjectTypeBuilder.groovy index fa7ae53b..c350b46f 100644 --- a/core/src/main/groovy/org/grails/gorm/graphql/types/output/AbstractObjectTypeBuilder.groovy +++ b/core/src/main/groovy/org/grails/gorm/graphql/types/output/AbstractObjectTypeBuilder.groovy @@ -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 @@ -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 arguments = prop.getArguments(typeManager, mapping) + if (!arguments.isEmpty()) { + field.arguments(arguments) + } + } + field + } + @Override GraphQLOutputType build(PersistentEntity entity) { @@ -85,7 +92,9 @@ abstract class AbstractObjectTypeBuilder implements ObjectTypeBuilder { List 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()) } } diff --git a/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/CustomType.groovy b/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/CustomType.groovy index d22c8f59..21aa908f 100644 --- a/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/CustomType.groovy +++ b/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/CustomType.groovy @@ -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) + } + } } } \ No newline at end of file diff --git a/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/SimpleType.groovy b/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/SimpleType.groovy index 66f90f5c..8f5dcc3b 100644 --- a/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/SimpleType.groovy +++ b/core/src/test/groovy/org/grails/gorm/graphql/domain/general/custom/property/SimpleType.groovy @@ -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) { diff --git a/docs/src/main/docs/guide/customizations/arguments.adoc b/docs/src/main/docs/guide/customizations/arguments.adoc new file mode 100644 index 00000000..790407f3 --- /dev/null +++ b/docs/src/main/docs/guide/customizations/arguments.adoc @@ -0,0 +1,91 @@ +== Arguments + +Arguments are how users supply data to your custom operation or field. Arguments support simple types (String, Integer, etc) as well as custom types that you define. + +[source,groovy] +---- +query('authorByName', Author) { + argument('name', String) //To take in a single string + + argument('names', [String]) //To take in a list of strings + + argument('fullName', 'AuthorNameArgument') { //A custom argument + accepts { + field('first', String) + field('last', String) + } + } +} +---- + +The API inside of the last argument block is exactly the same as the API for defining link:#custom[custom properties] with custom types. + +=== Argument Metadata + +GraphQL has the ability to store metadata about arguments to operations. + +[source,groovy] +---- +query('authorByName', Author) { + argument('name', String) { + defaultValue 'John' //Supply a sensible default + + nullable true //Allow a null value (default false) + + description 'The name of the author to search for' + } +} +---- +=== Using Argument Inputs +Within your supplied DataFetcher, arguments can easily be retrieved from their corresponding DataFetchingEnvironment and used as necessary. + +[source,groovy] +---- +class Author { + + String name + + static hasMany = [books: Book] + + static graphql = GraphQLMapping.build { + query('authorByName', Author) { + argument('name', String) + dataFetcher(new DataFetcher<>() { + @Override + Object get(DataFetchingEnvironment environment) { + Author.findByName(environment.getArgument('name')) + } + }) + } + add('booksPublishedBetween', [Book]) { + input(false) + argument('from', Date) + argument('to', Date) + dataFetcher { Author author, ClosureDataFetchingEnvironment environment -> + Date from = environment.getArgument('from') + Date to = environment.getArgument('to') + Book.where { + author == author + published >= from + published <= to + }.list() + } + } + } +} +---- + +The above example would support the following type of query: + +[source,graphql] +---- +query { + authorByName(name: "Herman Melville") { + booksPublishedBetween(from: "1850-01-01", to: "1852-12-31") { + title + pageCount + } + } +} +---- + diff --git a/docs/src/main/docs/guide/customizations/operations.adoc b/docs/src/main/docs/guide/customizations/operations.adoc index 36db563d..c2c52022 100644 --- a/docs/src/main/docs/guide/customizations/operations.adoc +++ b/docs/src/main/docs/guide/customizations/operations.adoc @@ -181,44 +181,6 @@ query('authorByName', Author) { //or [Author] to return a list of authors } ---- -=== Arguments - -Arguments are the way users can supply data to your operation. The argument can be a simple type (String, Integer, etc), or it can also be a custom type that you define. - -[source,groovy] ----- -query('authorByName', Author) { - argument('name', String) //To take in a single string - - argument('names', [String]) //To take in a list of strings - - argument('name', 'AuthorNameArgument') { //A custom argument - accepts { - field('first', String) - field('last', String) - } - } -} ----- - -The API inside of the last argument block is exactly the same as the API for defining link:#custom[custom properties] with custom types. - -==== Argument Metadata - -GraphQL has the ability to store metadata about arguments to operations. - -[source,groovy] ----- -query('authorByName', Author) { - argument('name', String) { - defaultValue 'John' //Supply a sensible default - - nullable true //Allow a null value (default false) - - description 'The name of the author to search for' - } -} ----- === Data Fetcher When creating a custom operation, it is necessary to supply a "data fetcher". The data fetcher is responsible for returning data to GraphQL to be used in generating the response. The data fetcher must be an instance of `graphql.schema.DataFetcher`. @@ -232,6 +194,7 @@ class Author { static hasMany = [books: Book] static graphql = GraphQLMapping.build { + argument('name', String) query('authorByName', Author) { dataFetcher(new DataFetcher<>() { @Override diff --git a/docs/src/main/docs/guide/toc.yml b/docs/src/main/docs/guide/toc.yml index 2d2ff87f..cfb92bd1 100644 --- a/docs/src/main/docs/guide/toc.yml +++ b/docs/src/main/docs/guide/toc.yml @@ -15,6 +15,7 @@ customizations: existing: GORM Properties custom: Custom Properties operations: Operations + arguments: Arguments responseHandlers: Validation Error and Delete Responses namingConvention: Naming Convention configuration: Configuration diff --git a/examples/grails-test-app/grails-app/domain/grails/test/app/ArguedField.groovy b/examples/grails-test-app/grails-app/domain/grails/test/app/ArguedField.groovy new file mode 100644 index 00000000..da3430de --- /dev/null +++ b/examples/grails-test-app/grails-app/domain/grails/test/app/ArguedField.groovy @@ -0,0 +1,44 @@ +package grails.test.app + +import grails.compiler.GrailsCompileStatic +import org.grails.gorm.graphql.entity.dsl.GraphQLMapping +import org.grails.gorm.graphql.fetcher.impl.ClosureDataFetchingEnvironment + +@GrailsCompileStatic +class ArguedField { + + String name + + static constraints = { + } + + static graphql = GraphQLMapping.build { + add('withArgument', String) { + input false + argument('ping', String) + dataFetcher { ArguedField af, ClosureDataFetchingEnvironment env -> + env.getArgument('ping') + } + } + add('withArgumentList', String) { + input false + argument('pings', [String]) + dataFetcher { ArguedField af, ClosureDataFetchingEnvironment env -> + List pings = (List)env.getArgument('pings') + "${pings.join('-')}" + } + } + add('withCustomArgument', String) { + input false + argument('ping', 'PingPong') { + accepts { + field('payload', String) + } + } + dataFetcher { ArguedField af, ClosureDataFetchingEnvironment env -> + Map ping = (Map)env.getArgument('ping') + ping["payload"] + } + } + } +} diff --git a/examples/grails-test-app/grails-app/domain/grails/test/app/UserRole.groovy b/examples/grails-test-app/grails-app/domain/grails/test/app/UserRole.groovy index 4bad2259..ef1123d1 100644 --- a/examples/grails-test-app/grails-app/domain/grails/test/app/UserRole.groovy +++ b/examples/grails-test-app/grails-app/domain/grails/test/app/UserRole.groovy @@ -1,9 +1,11 @@ package grails.test.app +import grails.compiler.GrailsCompileStatic import grails.gorm.DetachedCriteria import org.codehaus.groovy.util.HashCodeHelper import org.grails.gorm.graphql.entity.dsl.GraphQLMapping +@GrailsCompileStatic class UserRole implements Serializable { private static final long serialVersionUID = 1 diff --git a/examples/grails-test-app/src/integration-test/groovy/grails/test/app/ArguedFieldIntegrationSpec.groovy b/examples/grails-test-app/src/integration-test/groovy/grails/test/app/ArguedFieldIntegrationSpec.groovy new file mode 100644 index 00000000..8b6f6c4d --- /dev/null +++ b/examples/grails-test-app/src/integration-test/groovy/grails/test/app/ArguedFieldIntegrationSpec.groovy @@ -0,0 +1,67 @@ +package grails.test.app + +import grails.testing.mixin.integration.Integration +import grails.testing.spock.OnceBefore +import org.grails.gorm.graphql.plugin.testing.GraphQLSpec +import spock.lang.Shared +import spock.lang.Specification + +@Integration +class ArguedFieldIntegrationSpec extends Specification implements GraphQLSpec { + + @Shared Long grailsId + + @OnceBefore + void createDomain() { + ArguedField.withTransaction { + grailsId = new ArguedField(name: 'test').save(flush: true).id + } + + } + + void "test a simple argument"() { + when: + def resp = graphQL.graphql(""" + { + arguedField(id: ${grailsId}) { + withArgument(ping: "PONG") + } + } + """) + def obj = resp.body().data.arguedField + + then: + obj.withArgument == "PONG" + } + + void "test a simple argument list"() { + when: + def resp = graphQL.graphql(""" + { + arguedField(id: ${grailsId}) { + withArgumentList(pings: ["P", "O", "N", "G" ]) + } + } + """) + def obj = resp.body().data.arguedField + + then: + obj.withArgumentList == "P-O-N-G" + } + + void "test a custom argument"() { + when: + def resp = graphQL.graphql(""" + { + arguedField(id: ${grailsId}) { + withCustomArgument(ping: {payload: "PONG"}) + } + } + """) + def obj = resp.body().data.arguedField + + then: + obj.withCustomArgument == "PONG" + } + +} diff --git a/gradle.properties b/gradle.properties index cfc95a1b..f80489e1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ title=GORM GraphQL authors=James Kleeh -projectVersion=2.0.1.BUILD-SNAPSHOT +projectVersion=2.0.2.BUILD-SNAPSHOT projectDesc=Generates a GraphQL schema based on entities in GORM projectUrl=https://github.com/grails/gorm-graphql githubSlug=grails/gorm-graphql diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 457aad0d..e708b1c0 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 833d3072..12d38de6 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Wed Dec 05 20:00:27 CET 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip diff --git a/gradlew b/gradlew index af6708ff..4f906e0c 100755 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,7 +44,7 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m"' +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" @@ -66,6 +82,7 @@ esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + # Determine the Java command to use to start the JVM. if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then @@ -109,10 +126,11 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` # We build the pattern for arguments to be converted via cygpath @@ -138,19 +156,19 @@ if $cygwin ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $i + 1` done case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi @@ -159,14 +177,9 @@ save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } -APP_ARGS=$(save "$@") +APP_ARGS=`save "$@"` # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index 0f8d5937..ac1b06f9 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -13,15 +29,18 @@ if "%DIRNAME%" == "" set DIRNAME=. set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init +if "%ERRORLEVEL%" == "0" goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -35,7 +54,7 @@ goto fail set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe -if exist "%JAVA_EXE%" goto init +if exist "%JAVA_EXE%" goto execute echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% @@ -45,28 +64,14 @@ echo location of your Java installation. goto fail -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - :execute @rem Setup the command line set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* :end @rem End local scope for the variables with windows NT shell