Skip to content

Using GraphQL Cursor Connections Specification at field (not root) level #1082

@sydneyhenrard

Description

@sydneyhenrard

I have a simple class that I want to expose via graphQL.

@Document(collection = "question")
public class Question {
    private String label;
}

I managed to expose it using a controller

@Controller
public class QuestionController {
    private final QuestionRepository questionRepository;

    public QuestionController(QuestionRepository questionRepository) {
        this.questionRepository = questionRepository;
    }

    @QueryMapping
    public Window<Question> allQuestions() {
        return questionRepository.findAllBy(ScrollPosition.offset());
    }
}

with schema

type Query {
    allQuestions: QuestionConnection
}
type Question {
    label: String
}

The next step is to expose all questions, along with possible tags (like search faceting). The questions should be paginated.
I created a wrapper class

public class WrappedQuestion {
    private Window<Question> questions;
    private List<String> tags;
}

Add a query mapping

@QueryMapping
public WrappedQuestion allQuestionsWithWrapper() {
    WrappedQuestion wrappedQuestion = new WrappedQuestion();
    wrappedQuestion.setQuestions(questionRepository.findAllBy(ScrollPosition.offset()));
    wrappedQuestion.setTags(List.of("Spring", "GraphQL"));
    return wrappedQuestion;
}

and its types

type Query {
    allQuestionsWithWrapper: WrappedQuestion
}

type WrappedQuestion {
    questions: QuestionConnection
    tags: [String]
}

When I run the following query, I got an error for the questions field:

query Wrapped{
  allQuestionsWithWrapper {
    questions {
      edges {
        node {
          label
        }
      }
    }
    tags    
  }
}
{
  "errors": [
    {
      "message": "The field at path '/allQuestionsWithWrapper/questions/edges' was declared as a non null type, but the code involved in retrieving data has wrongly returned a null value.  The graphql specification requires that the parent field be set to null, or if that is non nullable that it bubble up null to its parent and so on. The non-nullable type is '[QuestionEdge]' within parent type 'QuestionConnection'",
      "path": [
        "allQuestionsWithWrapper",
        "questions",
        "edges"
      ],
      "extensions": {
        "classification": "NullValueInNonNullableField"
      }
    }
  ],
  "data": {
    "allQuestionsWithWrapper": {
      "tags": [
        "Spring",
        "GraphQL"
      ]
    }
  }
}

The same query without wrapping works and returns data

query Simple {
  allQuestions  {
    edges {
      node {
        label
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions