|
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 | + @Autowired |
| 21 | + private GraphQLToolsProperties props; |
| 22 | + |
| 23 | + @Override |
| 24 | + public List<String> schemaStrings() throws IOException { |
| 25 | + Resource[] resources = applicationContext.getResources("classpath*:" + props.getSchemaLocationPattern()); |
| 26 | + if (resources.length <= 0) { |
| 27 | + throw new IllegalStateException( |
| 28 | + "No graphql schema files found on classpath with location pattern '" |
| 29 | + + props.getSchemaLocationPattern() |
| 30 | + + "'. Please add a graphql schema to the classpath or add a SchemaParser bean to your application context."); |
| 31 | + } |
| 32 | + |
| 33 | + return Arrays.stream(resources) |
| 34 | + .map(this::readSchema) |
| 35 | + .collect(Collectors.toList()); |
32 | 36 | } |
33 | 37 |
|
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); |
| 38 | + private String readSchema(Resource resource) { |
| 39 | + StringWriter writer = new StringWriter(); |
| 40 | + try (InputStream inputStream = resource.getInputStream()) { |
| 41 | + IOUtils.copy(inputStream, writer, StandardCharsets.UTF_8); |
| 42 | + } catch (IOException e) { |
| 43 | + throw new IllegalStateException("Cannot read graphql schema from resource " + resource, e); |
| 44 | + } |
| 45 | + return writer.toString(); |
45 | 46 | } |
46 | | - return writer.toString(); |
47 | | - } |
48 | 47 |
|
49 | 48 | } |
0 commit comments