|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.graphql.execution; |
| 17 | + |
| 18 | +import java.util.LinkedHashSet; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.function.Function; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import graphql.language.FieldDefinition; |
| 24 | +import graphql.language.ImplementingTypeDefinition; |
| 25 | +import graphql.language.ListType; |
| 26 | +import graphql.language.NonNullType; |
| 27 | +import graphql.language.ObjectTypeDefinition; |
| 28 | +import graphql.language.Type; |
| 29 | +import graphql.language.TypeName; |
| 30 | +import graphql.schema.idl.TypeDefinitionRegistry; |
| 31 | + |
| 32 | +/** |
| 33 | + * Exposes the {@link #generateConnectionTypes(TypeDefinitionRegistry) |
| 34 | + * generateConnectionTypes method} for adding boilerplate type definitions to a |
| 35 | + * {@link TypeDefinitionRegistry}, for pagination based on the Relay |
| 36 | + * <a href="https://relay.dev/graphql/connections.htm">GraphQL Cursor Connections Specification</a>. |
| 37 | + * |
| 38 | + * <p>Use {@link GraphQlSource.SchemaResourceBuilder#configureTypeDefinitionRegistry(Function)} |
| 39 | + * to enable connection type generation. |
| 40 | + * |
| 41 | + * @author Rossen Stoyanchev |
| 42 | + * @since 1.2 |
| 43 | + */ |
| 44 | +public class ConnectionTypeGenerator { |
| 45 | + |
| 46 | + private static final TypeName STRING_TYPE = new TypeName("String"); |
| 47 | + |
| 48 | + private static final TypeName BOOLEAN_TYPE = new TypeName("Boolean"); |
| 49 | + |
| 50 | + private static final TypeName PAGE_INFO_TYPE = new TypeName("PageInfo"); |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * Find fields whose type definition name ends in "Connection", considered |
| 55 | + * by the spec to be a {@literal Connection Type}, and add type definitions |
| 56 | + * for all such types, if they don't exist already. |
| 57 | + * @param registry the registry to check and add types to |
| 58 | + * @return the same registry instance with additional types added |
| 59 | + */ |
| 60 | + public TypeDefinitionRegistry generateConnectionTypes(TypeDefinitionRegistry registry) { |
| 61 | + |
| 62 | + Set<String> typeNames = findConnectionTypeNames(registry); |
| 63 | + |
| 64 | + if (!typeNames.isEmpty()) { |
| 65 | + registry.add(ObjectTypeDefinition.newObjectTypeDefinition() |
| 66 | + .name(PAGE_INFO_TYPE.getName()) |
| 67 | + .fieldDefinition(initFieldDefinition("hasPreviousPage", new NonNullType(BOOLEAN_TYPE))) |
| 68 | + .fieldDefinition(initFieldDefinition("hasNextPage", new NonNullType(BOOLEAN_TYPE))) |
| 69 | + .fieldDefinition(initFieldDefinition("startCursor", STRING_TYPE)) |
| 70 | + .fieldDefinition(initFieldDefinition("endCursor", STRING_TYPE)) |
| 71 | + .build()); |
| 72 | + |
| 73 | + typeNames.forEach(typeName -> { |
| 74 | + |
| 75 | + System.out.println("Generating pagination types for '" + typeName + "'"); |
| 76 | + String connectionTypeName = typeName + "Connection"; |
| 77 | + String edgeTypeName = typeName + "Edge"; |
| 78 | + |
| 79 | + registry.add(ObjectTypeDefinition.newObjectTypeDefinition() |
| 80 | + .name(connectionTypeName) |
| 81 | + .fieldDefinition(initFieldDefinition("edges", new NonNullType(new ListType(new TypeName(edgeTypeName))))) |
| 82 | + .fieldDefinition(initFieldDefinition("pageInfo", new NonNullType(PAGE_INFO_TYPE))) |
| 83 | + .build()); |
| 84 | + |
| 85 | + registry.add(ObjectTypeDefinition.newObjectTypeDefinition() |
| 86 | + .name(edgeTypeName) |
| 87 | + .fieldDefinition(initFieldDefinition("cursor", new NonNullType(STRING_TYPE))) |
| 88 | + .fieldDefinition(initFieldDefinition("node", new NonNullType(new TypeName(typeName)))) |
| 89 | + .build()); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + return registry; |
| 94 | + } |
| 95 | + |
| 96 | + private static Set<String> findConnectionTypeNames(TypeDefinitionRegistry registry) { |
| 97 | + return registry.types().values().stream() |
| 98 | + .filter(definition -> definition instanceof ImplementingTypeDefinition) |
| 99 | + .flatMap(definition -> { |
| 100 | + ImplementingTypeDefinition<?> typeDefinition = (ImplementingTypeDefinition<?>) definition; |
| 101 | + return typeDefinition.getFieldDefinitions().stream() |
| 102 | + .map(fieldDefinition -> { |
| 103 | + Type<?> type = fieldDefinition.getType(); |
| 104 | + return (type instanceof NonNullType ? ((NonNullType) type).getType() : type); |
| 105 | + }) |
| 106 | + .filter(type -> type instanceof TypeName) |
| 107 | + .map(type -> ((TypeName) type).getName()) |
| 108 | + .filter(name -> name.endsWith("Connection")) |
| 109 | + .filter(name -> registry.getType(name).isEmpty()) |
| 110 | + .map(name -> name.substring(0, name.length() - "Connection".length())); |
| 111 | + }) |
| 112 | + .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 113 | + } |
| 114 | + |
| 115 | + private FieldDefinition initFieldDefinition(String name, Type<?> returnType) { |
| 116 | + return FieldDefinition.newFieldDefinition().name(name).type(returnType).build(); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments