View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.graphql.schema;
23  
24  import org.opencastproject.graphql.directive.RolesAllowed;
25  import org.opencastproject.graphql.provider.GraphQLAdditionalTypeProvider;
26  import org.opencastproject.graphql.provider.GraphQLCodeRegistryProvider;
27  import org.opencastproject.graphql.provider.GraphQLDynamicTypeProvider;
28  import org.opencastproject.graphql.provider.GraphQLExtensionProvider;
29  import org.opencastproject.graphql.provider.GraphQLFieldVisibilityProvider;
30  import org.opencastproject.graphql.provider.GraphQLMutationProvider;
31  import org.opencastproject.graphql.provider.GraphQLQueryProvider;
32  import org.opencastproject.graphql.provider.GraphQLTypeFunctionProvider;
33  import org.opencastproject.graphql.schema.builder.AdditionalTypeBuilder;
34  import org.opencastproject.graphql.schema.builder.DynamicTypeBuilder;
35  import org.opencastproject.graphql.schema.builder.ExtensionBuilder;
36  import org.opencastproject.graphql.schema.builder.TypeFunctionBuilder;
37  import org.opencastproject.graphql.type.input.Mutation;
38  import org.opencastproject.graphql.type.output.Query;
39  import org.opencastproject.security.api.Organization;
40  
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import java.util.List;
45  import java.util.Objects;
46  
47  import graphql.annotations.AnnotationsSchemaCreator;
48  import graphql.annotations.processor.GraphQLAnnotations;
49  import graphql.schema.GraphQLSchema;
50  
51  public class SchemaBuilder {
52  
53    private static final Logger logger = LoggerFactory.getLogger(SchemaBuilder.class);
54  
55    private GraphQLAnnotations annotations;
56  
57    private final Organization organization;
58  
59    private final ExtensionBuilder extensionBuilder;
60  
61    private final DynamicTypeBuilder dynamicTypeBuilder;
62  
63    private final AdditionalTypeBuilder additionalTypeBuilder;
64  
65    private final TypeFunctionBuilder typeFunctionBuilder;
66  
67    public SchemaBuilder(Organization organization) {
68      Objects.requireNonNull(organization, "organization cannot be null");
69      this.organization = organization;
70      this.dynamicTypeBuilder = new DynamicTypeBuilder(organization);
71      this.extensionBuilder = new ExtensionBuilder();
72      this.additionalTypeBuilder = new AdditionalTypeBuilder();
73      this.typeFunctionBuilder = new TypeFunctionBuilder();
74    }
75  
76    public GraphQLSchema build() {
77      this.annotations = new GraphQLAnnotations();
78      this.annotations.getContainer().setInputPrefix("");
79      this.annotations.getContainer().setInputSuffix("");
80  
81      final var builder = GraphQLSchema.newSchema();
82  
83      typeFunctionBuilder.withAnnotations(annotations).build();
84  
85      extensionBuilder.withAnnotations(annotations).build();
86  
87      dynamicTypeBuilder.withAnnotations(annotations).build();
88  
89      final var annotationsSchema = AnnotationsSchemaCreator.newAnnotationsSchema();
90      additionalTypeBuilder.withAnnotationSchema(annotationsSchema).build();
91  
92      return annotationsSchema
93          .setGraphQLSchemaBuilder(builder)
94          .query(Query.class)
95          .mutation(Mutation.class)
96          .setAnnotationsProcessor(this.annotations)
97          .directive(RolesAllowed.class)
98          .build();
99    }
100 
101   public SchemaBuilder extensionProviders(List<GraphQLExtensionProvider> extensionProviders) {
102     extensionBuilder.withExtensionProviders(extensionProviders);
103     return this;
104   }
105 
106   public SchemaBuilder dynamicTypeProviders(List<GraphQLDynamicTypeProvider> dynamicTypeProviders) {
107     dynamicTypeBuilder.withDynamicTypeProviders(dynamicTypeProviders);
108     return this;
109   }
110 
111   public SchemaBuilder queryProviders(List<GraphQLQueryProvider> queryProviders) {
112     return this;
113   }
114 
115   public SchemaBuilder mutationProviders(List<GraphQLMutationProvider> mutationProviders) {
116     return this;
117   }
118 
119   public SchemaBuilder codeRegistryProviders(List<GraphQLCodeRegistryProvider> codeRegistryProviders) {
120     return this;
121   }
122 
123   public SchemaBuilder additionalTypeProviders(List<GraphQLAdditionalTypeProvider> additionalTypesProviders) {
124     additionalTypeBuilder.withAdditionalTypeProviders(additionalTypesProviders);
125     return this;
126   }
127 
128   public SchemaBuilder fieldVisibilityProviders(List<GraphQLFieldVisibilityProvider> fieldVisibilityProviders) {
129     return this;
130   }
131 
132   public SchemaBuilder typeFunctionProviders(List<GraphQLTypeFunctionProvider> typeFunctionProviders) {
133     typeFunctionBuilder.withTypeFunctionProviders(typeFunctionProviders);
134     return this;
135   }
136 }