Skip to content

Commit

Permalink
#175: add separate 'person2' model to mock-up position/organization a…
Browse files Browse the repository at this point in the history
…s nested objects
  • Loading branch information
Rob Nelson committed Jan 30, 2020
1 parent e767e2a commit 8fd749a
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package edu.tamu.scholars.middleware.graphql.inert.model.person;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

import java.util.List;

import edu.tamu.scholars.middleware.discovery.model.Common;
import edu.tamu.scholars.middleware.graphql.model.AbstractNestedDocument;
import io.leangen.graphql.annotations.types.GraphQLType;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

import com.fasterxml.jackson.annotation.JsonInclude;

import edu.tamu.scholars.middleware.discovery.annotation.CollectionSource;
import edu.tamu.scholars.middleware.discovery.annotation.NestedMultiValuedProperty;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject.Reference;
import edu.tamu.scholars.middleware.discovery.annotation.PropertySource;

import edu.tamu.scholars.middleware.graphql.inert.model.position.Organization;
import io.leangen.graphql.annotations.GraphQLIgnore;

// @GraphQLIgnore
@GraphQLType(
name = "CustomPerson"
)

@JsonInclude(NON_EMPTY)
public class Person extends AbstractNestedDocument {
private String name;
private String[] type;
private PositionStub[] positionStubs;

public Person() {
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String[] getType() {
return this.type;
}

public void setType(String[] type) {
this.type = type;
}

@GraphQLIgnore
public PositionStub[] getPositions() {
return this.positionStubs;
}

@GraphQLIgnore
public void setPositions(PositionStub[] pos) {
this.positionStubs = pos;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject.Reference;
import edu.tamu.scholars.middleware.discovery.annotation.PropertySource;
// import io.leangen.graphql.annotations.GraphQLIgnore;

import edu.tamu.scholars.middleware.graphql.inert.model.position.OrganizationStub;
import io.leangen.graphql.annotations.GraphQLIgnore;

// @GraphQLIgnore
@GraphQLType(
Expand All @@ -28,22 +30,37 @@
public class Position extends AbstractNestedDocument {
private String title;
private String[] type;
private OrganizationStub[] organizations;

public Position() {
}

public String getTitle() {
return title;
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

public String[] getType() {
return type;
return this.type;
}

public void setType(String[] type) {
this.type = type;
}


@GraphQLIgnore
public OrganizationStub[] getOrganization() {
return this.organizations;
}

@GraphQLIgnore
public void setOrganization(OrganizationStub[] orgs) {
this.organizations = orgs;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package edu.tamu.scholars.middleware.graphql.inert.model.person;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

import java.util.List;

import edu.tamu.scholars.middleware.discovery.model.Common;
import edu.tamu.scholars.middleware.graphql.model.AbstractNestedDocument;
import io.leangen.graphql.annotations.types.GraphQLType;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

import com.fasterxml.jackson.annotation.JsonInclude;

import edu.tamu.scholars.middleware.discovery.annotation.CollectionSource;
import edu.tamu.scholars.middleware.discovery.annotation.NestedMultiValuedProperty;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject.Reference;
import edu.tamu.scholars.middleware.discovery.annotation.PropertySource;

//import edu.tamu.scholars.middleware.graphql.inert.model.position.Organization;
import edu.tamu.scholars.middleware.graphql.inert.model.position.OrganizationStub;

import io.leangen.graphql.annotations.GraphQLIgnore;

@GraphQLIgnore
@JsonInclude(NON_EMPTY)
public class PositionStub extends AbstractNestedDocument {
private String id;
private String label;
private String[] type;
private OrganizationStub[] organizations;

public PositionStub() {
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String[] getType() {
return this.getType();
}

public void setType(String[] type) {
this.type = type;
}

public OrganizationStub[] getOrganizations() {
return this.organizations;
}

public void setOrganizations(OrganizationStub[] orgs) {
this.organizations = orgs;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package edu.tamu.scholars.middleware.graphql.inert.model.position;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

import java.util.List;

import edu.tamu.scholars.middleware.discovery.model.Common;
import edu.tamu.scholars.middleware.graphql.model.AbstractNestedDocument;
import io.leangen.graphql.annotations.types.GraphQLType;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

import com.fasterxml.jackson.annotation.JsonInclude;

import edu.tamu.scholars.middleware.discovery.annotation.CollectionSource;
import edu.tamu.scholars.middleware.discovery.annotation.NestedMultiValuedProperty;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject.Reference;
import edu.tamu.scholars.middleware.discovery.annotation.PropertySource;
// import io.leangen.graphql.annotations.GraphQLIgnore;

// @GraphQLIgnore
@GraphQLType(
name = "CustomOrganization"
)

@JsonInclude(NON_EMPTY)
public class Organization extends AbstractNestedDocument {
private String name;
private String[] type;

public Organization() {
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String[] getType() {
return type;
}

public void setType(String[] type) {
this.type = type;
}

// TODO: organizationWithin ...
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package edu.tamu.scholars.middleware.graphql.inert.model.position;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY;

import java.util.List;

import edu.tamu.scholars.middleware.discovery.model.Common;
import edu.tamu.scholars.middleware.graphql.model.AbstractNestedDocument;
import io.leangen.graphql.annotations.types.GraphQLType;
import org.springframework.data.solr.core.mapping.Indexed;
import org.springframework.data.solr.core.mapping.SolrDocument;

import com.fasterxml.jackson.annotation.JsonInclude;

import edu.tamu.scholars.middleware.discovery.annotation.CollectionSource;
import edu.tamu.scholars.middleware.discovery.annotation.NestedMultiValuedProperty;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject;
import edu.tamu.scholars.middleware.discovery.annotation.NestedObject.Reference;
import edu.tamu.scholars.middleware.discovery.annotation.PropertySource;

import edu.tamu.scholars.middleware.graphql.inert.model.position.Organization;
import io.leangen.graphql.annotations.GraphQLIgnore;

@GraphQLIgnore
@JsonInclude(NON_EMPTY)
public class OrganizationStub extends AbstractNestedDocument {
private String id;
private String label;
private String[] type;

public OrganizationStub() {
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String getLabel() {
return this.label;
}

public void setTitle(String label) {
this.label = label;
}

public String[] getType() {
return this.type;
}

public void setType(String[] type) {
this.type = type;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package edu.tamu.scholars.middleware.graphql.inert.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.tamu.scholars.middleware.discovery.model.Individual;
import edu.tamu.scholars.middleware.discovery.model.repo.IndividualRepo;
// import edu.tamu.scholars.middleware.graphql.model.Collection;
import graphql.language.Field;
import io.leangen.graphql.annotations.GraphQLArgument;
import io.leangen.graphql.annotations.GraphQLEnvironment;
import io.leangen.graphql.annotations.GraphQLQuery;
import io.leangen.graphql.annotations.GraphQLContext;
import io.leangen.graphql.spqr.spring.annotations.GraphQLApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import edu.tamu.scholars.middleware.graphql.inert.model.position.Organization;
import edu.tamu.scholars.middleware.graphql.inert.model.position.OrganizationStub;
import edu.tamu.scholars.middleware.graphql.inert.model.person.Position;
import java.util.List;
import java.util.Optional;

@GraphQLApi
@Service
public class OrganizationResolver {
@Autowired
private IndividualRepo repo;

@Autowired
private ObjectMapper mapper;

@GraphQLQuery(name = "organization")
// @formatter:off
public Organization getById(
@GraphQLArgument(name = "id") String id,
@GraphQLEnvironment List<Field> fields
) {
// @formatter:on


Optional<Individual> organization = repo.findById(id);
ObjectNode node = mapper.valueToTree(organization);
System.out.println("**** NODE: " + node);
return mapper.convertValue(node, Organization.class);
}



@GraphQLQuery
public Organization[] organizations(@GraphQLContext Position position) {
OrganizationStub[] organizations = position.getOrganization();
Organization[] list = new Organization[organizations.length];

for (int i = 0; i < organizations.length; i++) {
OrganizationStub orgStub = organizations[i];
Optional<Individual> org = repo.findById(orgStub.getId());
ObjectNode node = mapper.valueToTree(org);
System.out.println("**** NODE: " + node);
Organization real = mapper.convertValue(node, Organization.class);
list[i] = real;
}
return list;
}





}
Loading

0 comments on commit 8fd749a

Please sign in to comment.