Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sydneyhenrard opened this issue Nov 6, 2024 · 1 comment
Labels
status: waiting-for-triage An issue we've not yet triaged

Comments

@sydneyhenrard
Copy link

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
      }
    }
  }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 6, 2024
@sydneyhenrard
Copy link
Author

I think I found the solution

    @QueryMapping
    public WrappedQuestion allQuestionsWithWrapper() {
        WrappedQuestion wrappedQuestion = new WrappedQuestion();
        return wrappedQuestion;
    }

    @SchemaMapping(field = "questions")
    public Window<Question> questions(WrappedQuestion wrappedQuestion) {
        return questionRepository.findAllBy(ScrollPosition.offset());
    }

    @SchemaMapping(field="tags")
    public List<String> tags(WrappedQuestion wrappedQuestion) {
        return List.of("Spring", "GraphQL");
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants