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

Invalid path reference articles.title; Associations can only be pointed to directly or via their id property #746

Closed
kliarist opened this issue Jul 11, 2023 · 6 comments
Labels
for: external-project Needs a change in external project

Comments

@kliarist
Copy link

kliarist commented Jul 11, 2023

I am using spring-graphql along with spring-data-mongo and querydsl and faced with the below issue

org.springframework.data.mapping.MappingException: Invalid path reference articles.title; Associations can only be pointed to directly or via their id property
	at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.getPath(QueryMapper.java:1275) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:1134) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:1111) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.convert.QueryMapper.createPropertyField(QueryMapper.java:365) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.convert.QueryMapper.lambda$mapFieldsToPropertyNames$0(QueryMapper.java:234) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at java.base/java.util.Map.forEach(Map.java:713) ~[na:na]
	at org.springframework.data.mongodb.core.convert.QueryMapper.mapFieldsToPropertyNames(QueryMapper.java:232) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedFields(QueryMapper.java:220) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.QueryOperations$QueryContext.getMappedFields(QueryOperations.java:363) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2573) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:177) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.all(ExecutableFindOperationSupport.java:135) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.data.mongodb.repository.support.SimpleMongoRepository$FluentQueryByExample.all(SimpleMongoRepository.java:391) ~[spring-data-mongodb-4.1.1.jar:4.1.1]
	at org.springframework.graphql.data.query.QueryByExampleDataFetcher$ManyEntityFetcher.getResult(QueryByExampleDataFetcher.java:751) ~[spring-graphql-1.2.1.jar:1.2.1]

Here is my set up :

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@EqualsAndHashCode
@QueryEntity
@Document(collection = "articles")
public class Article {

    @Id
    private String id;
    private String title;
    private Integer minutesRead;
    @DBRef(lazy = true)
    private User author;
}
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode
@Builder
@QueryEntity
@Document(collection = "users")
public class User {

    @Id
    private String id;

    private String name;
    private Integer age;
    private Date createdAt;
    private String nationality;
    private List<String> friendsIds;
    @DBRef(lazy = true)
    private List<Article> articles;
}
@GraphQlRepository
public interface UserRepository extends MongoRepository<User, String>, QuerydslPredicateExecutor<User> {
}

here is my graphql schema:

type Query {
    users: [User]
     ...
}

👍 So while the following graphql seems to work fine:

query {
  users {
    id
    age
    name
    articles {
      id
    }
  }
}	

❌ adding extra properties on the nested articles object does not:

query {
  users {
    id
    age
    name
    articles {
      id
      title
    }
  }
}	

throwing org.springframework.data.mapping.MappingException: Invalid path reference articles.title; Associations can only be pointed to directly or via their id property.

Can you please shed some light as to what is the problem here ?

Also, I have noticed that when using MongoRepository along with QuerydslPredicateExecutor then QueryByExampleDataFetcher takes precedence over QuerydslDataFetcher. Is this expected behaviour?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 11, 2023
@bclozel
Copy link
Member

bclozel commented Jul 11, 2023

Inline code snippets don't really help here. Can you share the full sample application that we can use to replicate the problem?

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Jul 11, 2023
@rstoyanchev
Copy link
Contributor

Also, I have noticed that when using MongoRepository along with QuerydslPredicateExecutor then QueryByExampleDataFetcher takes precedence over QuerydslDataFetcher. Is this expected behaviour?

We generally don't expect both, and if both are implemented the order is not defined.

@kliarist
Copy link
Author

kliarist commented Jul 11, 2023

Inline code snippets don't really help here. Can you share the full sample application that we can use to replicate the problem?

Thanks for the quick response. Sure. here is a sample project to reproduce it.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 11, 2023
@kliarist
Copy link
Author

Also, I have noticed that when using MongoRepository along with QuerydslPredicateExecutor then QueryByExampleDataFetcher takes precedence over QuerydslDataFetcher. Is this expected behaviour?

We generally don't expect both, and if both are implemented the order is not defined.

Thanks. I see.. I need the QuerydslPredicateExecutor present tho, as I will need the predicate builder functionality in the future.

@mp911de
Copy link
Member

mp911de commented Jul 17, 2023

This is a Spring Data problem when attempting to project properties from an association. I filed spring-projects/spring-data-mongodb#4453 to address the issue.

@mp911de mp911de added for: external-project Needs a change in external project and removed status: feedback-provided Feedback has been provided status: waiting-for-triage An issue we've not yet triaged labels Jul 17, 2023
@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2023
@kliarist
Copy link
Author

@mp911de I see.. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a change in external project
Projects
None yet
Development

No branches or pull requests

5 participants