You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@grails.apache.org by bo...@apache.org on 2026/08/23 16:04:18 UTC

(grails-core) branch refactor/graphql-testing-util created (now b4c7b5c926)

This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a change to branch refactor/graphql-testing-util
in repository https://gitbox.apache.org/repos/asf/grails-core.git


      at b4c7b5c926 Honor an explicitly set mergedField in MockDataFetchingEnvironment

This branch includes the following new commits:

     new b4c7b5c926 Honor an explicitly set mergedField in MockDataFetchingEnvironment

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(grails-core) 01/01: Honor an explicitly set mergedField in MockDataFetchingEnvironment

Posted by bo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

borinquenkid pushed a commit to branch refactor/graphql-testing-util
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit b4c7b5c9263f2f2728c0a597d7f25cc909165dc4
Author: Walter Duque de Estrada <wb...@mac.com>
AuthorDate: Sun Aug 23 09:15:37 2026 -0500

    Honor an explicitly set mergedField in MockDataFetchingEnvironment
    
    getMergedField() always rebuilt a MergedField from `fields`, ignoring
    the settable `mergedField` property entirely - it was never read
    anywhere. Fall back to building from fields only when mergedField
    isn't set, so test authors can stub a MergedField directly (e.g. one
    with a custom name/alias that the fields-list constructor path can't
    express) instead of only being able to set it indirectly via fields.
    
    Added MockDataFetchingEnvironmentSpec, the first dedicated spec for
    this public testing utility, covering both the fields-derived default
    and the explicit-mergedField override.
    
    Co-Authored-By: Claude Sonnet 5 <no...@anthropic.com>
---
 .../testing/MockDataFetchingEnvironment.groovy     |  4 +-
 .../testing/MockDataFetchingEnvironmentSpec.groovy | 47 ++++++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy
index e47955e589..2e2185bcd1 100644
--- a/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy
+++ b/grails-data-graphql/core/src/main/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironment.groovy
@@ -93,7 +93,7 @@ class MockDataFetchingEnvironment implements DataFetchingEnvironment {
 
     @Override
     MergedField getMergedField() {
-        MergedField.newMergedField(fields).build()
+        mergedField ?: MergedField.newMergedField(fields).build()
     }
 
     @Override
@@ -102,7 +102,7 @@ class MockDataFetchingEnvironment implements DataFetchingEnvironment {
     }
 
     @Override
-    def <K, V> DataLoader<K,V> getDataLoader(String dataLoaderName) {
+    <K, V> DataLoader<K,V> getDataLoader(String dataLoaderName) {
         dataLoaderRegistry ? dataLoaderRegistry.getDataLoader(dataLoaderName) : null
     }
 
diff --git a/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironmentSpec.groovy b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironmentSpec.groovy
new file mode 100644
index 0000000000..a54c7f2825
--- /dev/null
+++ b/grails-data-graphql/core/src/test/groovy/org/grails/gorm/graphql/testing/MockDataFetchingEnvironmentSpec.groovy
@@ -0,0 +1,47 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.
+ */
+
+package org.grails.gorm.graphql.testing
+
+import graphql.execution.MergedField
+import graphql.language.Field
+import spock.lang.Specification
+
+class MockDataFetchingEnvironmentSpec extends Specification {
+
+    void "test getMergedField builds a MergedField from fields when none is set explicitly"() {
+        given:
+        Field one = Field.newField('one').build()
+        Field two = Field.newField('two').build()
+        MockDataFetchingEnvironment environment = new MockDataFetchingEnvironment(fields: [one, two])
+
+        expect:
+        environment.getMergedField().fields == [one, two]
+    }
+
+    void "test getMergedField returns the explicitly set MergedField instead of building one from fields"() {
+        given:
+        Field ignored = Field.newField('ignored').build()
+        MergedField explicit = MergedField.newMergedField(Field.newField('explicit').build()).build()
+        MockDataFetchingEnvironment environment = new MockDataFetchingEnvironment(fields: [ignored], mergedField: explicit)
+
+        expect:
+        environment.getMergedField().is(explicit)
+    }
+}