|
2 | 2 |
|
3 | 3 | import org.apache.commons.io.IOUtils;
|
4 | 4 | import org.springframework.beans.factory.annotation.Autowired;
|
5 |
| -import org.springframework.beans.factory.annotation.Value; |
6 | 5 | import org.springframework.context.ApplicationContext;
|
7 | 6 | import org.springframework.core.io.Resource;
|
8 | 7 |
|
|
16 | 15 |
|
17 | 16 | public class ClasspathResourceSchemaStringProvider implements SchemaStringProvider {
|
18 | 17 |
|
19 |
| - @Autowired |
20 |
| - private ApplicationContext applicationContext; |
21 |
| - @Value("${graphql.tools.schemaLocationPattern:**/*.graphqls}") |
22 |
| - private String schemaLocationPattern; |
23 |
| - |
24 |
| - @Override |
25 |
| - public List<String> schemaStrings() throws IOException { |
26 |
| - Resource[] resources = applicationContext.getResources("classpath*:" + schemaLocationPattern); |
27 |
| - if (resources.length <= 0) { |
28 |
| - throw new IllegalStateException( |
29 |
| - "No graphql schema files found on classpath with location pattern '" |
30 |
| - + schemaLocationPattern |
31 |
| - + "'. Please add a graphql schema to the classpath or add a SchemaParser bean to your application context."); |
| 18 | + @Autowired |
| 19 | + private ApplicationContext applicationContext; |
| 20 | + private String schemaLocationPattern; |
| 21 | + |
| 22 | + public ClasspathResourceSchemaStringProvider(String schemaLocationPattern) { |
| 23 | + this.schemaLocationPattern = schemaLocationPattern; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public List<String> schemaStrings() throws IOException { |
| 28 | + Resource[] resources = applicationContext.getResources("classpath*:" + schemaLocationPattern); |
| 29 | + if (resources.length <= 0) { |
| 30 | + throw new IllegalStateException( |
| 31 | + "No graphql schema files found on classpath with location pattern '" |
| 32 | + + schemaLocationPattern |
| 33 | + + "'. Please add a graphql schema to the classpath or add a SchemaParser bean to your application context."); |
| 34 | + } |
| 35 | + |
| 36 | + return Arrays.stream(resources) |
| 37 | + .map(this::readSchema) |
| 38 | + .collect(Collectors.toList()); |
32 | 39 | }
|
33 | 40 |
|
34 |
| - return Arrays.stream(resources) |
35 |
| - .map(this::readSchema) |
36 |
| - .collect(Collectors.toList()); |
37 |
| - } |
38 |
| - |
39 |
| - private String readSchema(Resource resource) { |
40 |
| - StringWriter writer = new StringWriter(); |
41 |
| - try (InputStream inputStream = resource.getInputStream()) { |
42 |
| - IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8); |
43 |
| - } catch (IOException e) { |
44 |
| - throw new IllegalStateException("Cannot read graphql schema from resource " + resource, e); |
| 41 | + private String readSchema(Resource resource) { |
| 42 | + StringWriter writer = new StringWriter(); |
| 43 | + try (InputStream inputStream = resource.getInputStream()) { |
| 44 | + IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8); |
| 45 | + } catch (IOException e) { |
| 46 | + throw new IllegalStateException("Cannot read graphql schema from resource " + resource, e); |
| 47 | + } |
| 48 | + return writer.toString(); |
45 | 49 | }
|
46 |
| - return writer.toString(); |
47 |
| - } |
48 | 50 |
|
49 | 51 | }
|
0 commit comments