-
Notifications
You must be signed in to change notification settings - Fork 0
GG-291 Make nodes and totalCount fields optional #376
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,6 +168,8 @@ public ProcessedSchema(TypeDefinitionRegistry typeRegistry) { | |
|
|
||
| federationIsImported = LinkDirectiveProcessor.loadFederationImportedDefinitions(typeRegistry) != null; | ||
| federationEntitiesExist = queryType != null && queryType.hasField(FEDERATION_ENTITIES_FIELD.getName()) && isFederationImported(); | ||
|
|
||
| markConnectionsWithTotalCountField(); | ||
| } | ||
|
|
||
| private SchemaDefinition createSchemaDefinition() { | ||
|
|
@@ -196,6 +198,21 @@ private SchemaDefinition createSchemaDefinition() { | |
| return definitionBuilder.build(); | ||
| } | ||
|
|
||
| /** | ||
| * Mark fields whose return type is a connection type that contains the {@code totalCount} field. This is used to | ||
| * determine whether to generate a count method or not through | ||
| * {@link no.sikt.graphitron.generators.db.FetchCountDBMethodGenerator}. | ||
| */ | ||
| private void markConnectionsWithTotalCountField() { | ||
| this.objects | ||
| .values() | ||
| .stream() | ||
| .flatMap(obj -> obj.getFields().stream()) | ||
| .filter(this::isConnectionObject) | ||
| .filter(this::hasTotalCountField) | ||
| .forEach(field -> field.setHasTotalCountFieldInReturnType(true)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Er litt skeptisk til å gjøre dette mutabelt på denne måten, det burde være nok å sjekke dette på selve connection-typen, men usikker på om det blir mer eller mindre komplekst. |
||
| } | ||
|
|
||
| public boolean nodeExists() { | ||
| return nodeExists; | ||
| } | ||
|
|
@@ -829,6 +846,16 @@ public boolean federationEntitiesExist() { | |
| return federationEntitiesExist; | ||
| } | ||
|
|
||
| /** | ||
| * @param field A field whose type is assumed to be a connection object present in {@link #connectionObjects}. | ||
| * @return Whether this field points to a connection object type that has a {@code totalCount} field. | ||
| */ | ||
| private boolean hasTotalCountField(FieldSpecification field) { | ||
| return connectionObjects | ||
| .get(field.getTypeName()) | ||
| .hasField(GraphQLReservedName.CONNECTION_TOTAL_COUNT.getName()); | ||
| } | ||
|
|
||
| /** | ||
| * @return All types which could potentially have tables. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,4 +146,23 @@ void queryAfterService() { | |
| "queryNormal() {return _iv_env -> {return" | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("When optional field 'totalCount' is included in the schema for a field having a service and pagination, generate the count method") | ||
| void serviceWithPaginationAndAllOptionalFieldsIncluded() { | ||
| assertGeneratedContentMatches( | ||
| "operation/withPaginationAndAllOptionalFieldsIncluded", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Det burde være nok å sjekke at count metoden kalles slik som i ResolverPaginationTest. |
||
| CUSTOMER_CONNECTION | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| @Test | ||
| @DisplayName("When optional field 'totalCount' is not included in the schema for a field having a service and pagination, do not generate the count method") | ||
| void serviceWithPaginationAndNoOptionalFieldsIncluded() { | ||
| assertGeneratedContentMatches( | ||
| "operation/withPaginationAndNoOptionalFieldsIncluded", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Samme her, nok å sjekke at den ikke kommer med. |
||
| CUSTOMER_CONNECTION_WITH_NO_OPTIONALS | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package no.sikt.graphitron.reducedgenerators; | ||
|
|
||
| import no.sikt.graphitron.definitions.objects.ObjectDefinition; | ||
| import no.sikt.graphitron.generators.db.DBClassGenerator; | ||
| import no.sikt.graphitron.generators.db.FetchCountDBMethodGenerator; | ||
| import no.sikt.graphitron.generators.db.FetchMappedObjectDBMethodGenerator; | ||
| import no.sikt.graphitron.generators.db.SelectHelperDBMethodGenerator; | ||
| import no.sikt.graphitron.javapoet.TypeSpec; | ||
| import no.sikt.graphql.schema.ProcessedSchema; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class PaginationOnlyDBClassGenerator extends DBClassGenerator { | ||
| public PaginationOnlyDBClassGenerator(ProcessedSchema processedSchema) { | ||
| super(processedSchema); | ||
| } | ||
|
|
||
| @Override | ||
| public TypeSpec generate(ObjectDefinition target) { | ||
| return getSpec( | ||
| target.getName(), | ||
| List.of(new FetchMappedObjectDBMethodGenerator(target, processedSchema), | ||
| new SelectHelperDBMethodGenerator(target, processedSchema), | ||
| new FetchCountDBMethodGenerator(target, processedSchema)) | ||
| ).build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| type CustomerConnection { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gi den gjerne et annet navn så de ikke risikerer å kollidere med de andre. For eksempel Det eneste som har noe å si er at den slutter på |
||
| edges: [CustomerConnectionEdge] | ||
| pageInfo: PageInfo | ||
| } | ||
|
|
||
| type CustomerConnectionEdge { | ||
| cursor: String | ||
| node: CustomerTable | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| type Query { | ||
| customers(first: Int = 100, after: String): CustomerConnection | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| type Query { | ||
| customers(first: Int = 100, after: String): CustomerConnection | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import fake.code.generated.transform.RecordTransformer; | ||
| import fake.graphql.example.model.CustomerTable; | ||
| import graphql.schema.DataFetcher; | ||
| import java.lang.Integer; | ||
| import java.lang.String; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import no.sikt.graphitron.codereferences.services.ResolverFetchService; | ||
| import no.sikt.graphql.helpers.resolvers.ResolverHelpers; | ||
| import no.sikt.graphql.helpers.resolvers.ServiceDataFetcherHelper; | ||
| import no.sikt.graphql.relay.ConnectionImpl; | ||
|
|
||
| public class QueryGeneratedDataFetcher { | ||
| public static DataFetcher<CompletableFuture<ConnectionImpl<CustomerTable>>> customers() { | ||
| return _iv_env -> { | ||
| Integer _mi_first = _iv_env.getArgument("first"); | ||
| String _mi_after = _iv_env.getArgument("after"); | ||
| int _iv_pageSize = ResolverHelpers.getPageSize(_mi_first, 1000, 100); | ||
| var _iv_transform = new RecordTransformer(_iv_env); | ||
| var _rs_resolverFetchService = new ResolverFetchService(_iv_transform.getCtx()); | ||
| return new ServiceDataFetcherHelper<>(_iv_transform).loadPaginated( | ||
| _iv_pageSize, | ||
| () -> _rs_resolverFetchService.queryList(_iv_pageSize, _mi_after), | ||
| (_iv_keys) -> _rs_resolverFetchService.countQueryList(), | ||
| (_iv_recordTransform, _iv_response) -> _iv_recordTransform.customerTableRecordToGraphType(_iv_response, "") | ||
| ); | ||
| } ; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| type Query { | ||
| customers(first: Int = 100, after: String): CustomerConnection @service( | ||
| service: {name: "RESOLVER_FETCH_SERVICE", method: "queryList"}) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.