|
| 1 | +package graphql.servlet; |
| 2 | + |
| 3 | +import graphql.schema.GraphQLSchema; |
| 4 | +import graphql.servlet.internal.GraphQLRequest; |
| 5 | + |
| 6 | +import javax.servlet.http.HttpServletRequest; |
| 7 | +import javax.websocket.server.HandshakeRequest; |
| 8 | +import java.util.List; |
| 9 | +import java.util.function.Supplier; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Andrew Potter |
| 13 | + */ |
| 14 | +public class GraphQLInvocationInputFactory { |
| 15 | + private final Supplier<GraphQLSchemaProvider> schemaProviderSupplier; |
| 16 | + private final Supplier<GraphQLContextBuilder> contextBuilderSupplier; |
| 17 | + private final Supplier<GraphQLRootObjectBuilder> rootObjectBuilderSupplier; |
| 18 | + |
| 19 | + protected GraphQLInvocationInputFactory(Supplier<GraphQLSchemaProvider> schemaProviderSupplier, Supplier<GraphQLContextBuilder> contextBuilderSupplier, Supplier<GraphQLRootObjectBuilder> rootObjectBuilderSupplier) { |
| 20 | + this.schemaProviderSupplier = schemaProviderSupplier; |
| 21 | + this.contextBuilderSupplier = contextBuilderSupplier; |
| 22 | + this.rootObjectBuilderSupplier = rootObjectBuilderSupplier; |
| 23 | + } |
| 24 | + |
| 25 | + public GraphQLSchemaProvider getSchemaProvider() { |
| 26 | + return schemaProviderSupplier.get(); |
| 27 | + } |
| 28 | + |
| 29 | + public GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest, HttpServletRequest request) { |
| 30 | + return create(graphQLRequest, request, false); |
| 31 | + } |
| 32 | + |
| 33 | + public GraphQLBatchedInvocationInput create(List<GraphQLRequest> graphQLRequests, HttpServletRequest request) { |
| 34 | + return create(graphQLRequests, request, false); |
| 35 | + } |
| 36 | + |
| 37 | + public GraphQLSingleInvocationInput createReadOnly(GraphQLRequest graphQLRequest, HttpServletRequest request) { |
| 38 | + return create(graphQLRequest, request, true); |
| 39 | + } |
| 40 | + |
| 41 | + public GraphQLBatchedInvocationInput createReadOnly(List<GraphQLRequest> graphQLRequests, HttpServletRequest request) { |
| 42 | + return create(graphQLRequests, request, true); |
| 43 | + } |
| 44 | + |
| 45 | + public GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest) { |
| 46 | + return new GraphQLSingleInvocationInput( |
| 47 | + graphQLRequest, |
| 48 | + schemaProviderSupplier.get().getSchema(), |
| 49 | + contextBuilderSupplier.get().build(), |
| 50 | + rootObjectBuilderSupplier.get().build() |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + private GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest, HttpServletRequest request, boolean readOnly) { |
| 55 | + return new GraphQLSingleInvocationInput( |
| 56 | + graphQLRequest, |
| 57 | + readOnly ? schemaProviderSupplier.get().getReadOnlySchema(request) : schemaProviderSupplier.get().getSchema(request), |
| 58 | + contextBuilderSupplier.get().build(request), |
| 59 | + rootObjectBuilderSupplier.get().build(request) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + private GraphQLBatchedInvocationInput create(List<GraphQLRequest> graphQLRequests, HttpServletRequest request, boolean readOnly) { |
| 64 | + return new GraphQLBatchedInvocationInput( |
| 65 | + graphQLRequests, |
| 66 | + readOnly ? schemaProviderSupplier.get().getReadOnlySchema(request) : schemaProviderSupplier.get().getSchema(request), |
| 67 | + contextBuilderSupplier.get().build(request), |
| 68 | + rootObjectBuilderSupplier.get().build(request) |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + public GraphQLSingleInvocationInput create(GraphQLRequest graphQLRequest, HandshakeRequest request) { |
| 73 | + return new GraphQLSingleInvocationInput( |
| 74 | + graphQLRequest, |
| 75 | + schemaProviderSupplier.get().getSchema(request), |
| 76 | + contextBuilderSupplier.get().build(request), |
| 77 | + rootObjectBuilderSupplier.get().build(request) |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public GraphQLBatchedInvocationInput create(List<GraphQLRequest> graphQLRequest, HandshakeRequest request) { |
| 82 | + return new GraphQLBatchedInvocationInput( |
| 83 | + graphQLRequest, |
| 84 | + schemaProviderSupplier.get().getSchema(request), |
| 85 | + contextBuilderSupplier.get().build(request), |
| 86 | + rootObjectBuilderSupplier.get().build(request) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + public static Builder newBuilder(GraphQLSchema schema) { |
| 91 | + return new Builder(new DefaultGraphQLSchemaProvider(schema)); |
| 92 | + } |
| 93 | + |
| 94 | + public static Builder newBuilder(GraphQLSchemaProvider schemaProvider) { |
| 95 | + return new Builder(schemaProvider); |
| 96 | + } |
| 97 | + |
| 98 | + public static Builder newBuilder(Supplier<GraphQLSchemaProvider> schemaProviderSupplier) { |
| 99 | + return new Builder(schemaProviderSupplier); |
| 100 | + } |
| 101 | + |
| 102 | + public static class Builder { |
| 103 | + private final Supplier<GraphQLSchemaProvider> schemaProviderSupplier; |
| 104 | + private Supplier<GraphQLContextBuilder> contextBuilderSupplier = DefaultGraphQLContextBuilder::new; |
| 105 | + private Supplier<GraphQLRootObjectBuilder> rootObjectBuilderSupplier = DefaultGraphQLRootObjectBuilder::new; |
| 106 | + |
| 107 | + public Builder(GraphQLSchemaProvider schemaProvider) { |
| 108 | + this(() -> schemaProvider); |
| 109 | + } |
| 110 | + |
| 111 | + public Builder(Supplier<GraphQLSchemaProvider> schemaProviderSupplier) { |
| 112 | + this.schemaProviderSupplier = schemaProviderSupplier; |
| 113 | + } |
| 114 | + |
| 115 | + public Builder withGraphQLContextBuilder(GraphQLContextBuilder contextBuilder) { |
| 116 | + return withGraphQLContextBuilder(() -> contextBuilder); |
| 117 | + } |
| 118 | + |
| 119 | + public Builder withGraphQLContextBuilder(Supplier<GraphQLContextBuilder> contextBuilderSupplier) { |
| 120 | + this.contextBuilderSupplier = contextBuilderSupplier; |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + public Builder withGraphQLRootObjectBuilder(GraphQLRootObjectBuilder rootObjectBuilder) { |
| 125 | + return withGraphQLRootObjectBuilder(() -> rootObjectBuilder); |
| 126 | + } |
| 127 | + |
| 128 | + public Builder withGraphQLRootObjectBuilder(Supplier<GraphQLRootObjectBuilder> rootObjectBuilderSupplier) { |
| 129 | + this.rootObjectBuilderSupplier = rootObjectBuilderSupplier; |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public GraphQLInvocationInputFactory build() { |
| 134 | + return new GraphQLInvocationInputFactory(schemaProviderSupplier, contextBuilderSupplier, rootObjectBuilderSupplier); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments