Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Can not register extended scalars. #307

Closed
jaggerwang opened this issue Dec 6, 2019 · 3 comments
Closed

Can not register extended scalars. #307

jaggerwang opened this issue Dec 6, 2019 · 3 comments
Labels

Comments

@jaggerwang
Copy link

When using JSON scalar type from (graphql-java-extended-scalars)[https://github.com/graphql-java/graphql-java-extended-scalars], I got the following error.

Caused by: com.coxautodev.graphql.tools.SchemaClassScannerError: Unable to match type definition (NonNullType{type=TypeName{name='JSON'}}) with java type (class net.jaggerwang.sbip.entity.FileEntity$Meta): No TypeDefinition for type name JSON
        at com.coxautodev.graphql.tools.TypeClassMatcher.error(TypeClassMatcher.kt:22) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.TypeClassMatcher.match(TypeClassMatcher.kt:91) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.TypeClassMatcher.match$default(TypeClassMatcher.kt:32) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.TypeClassMatcher.match(TypeClassMatcher.kt:75) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.TypeClassMatcher.match(TypeClassMatcher.kt:28) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.SchemaClassScanner.scanResolverInfoForPotentialMatches(SchemaClassScanner.kt:268) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.SchemaClassScanner.scanQueueItemForPotentialMatches(SchemaClassScanner.kt:251) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.SchemaClassScanner.scanQueue(SchemaClassScanner.kt:103) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.SchemaClassScanner.scanForClasses(SchemaClassScanner.kt:81) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.SchemaParserBuilder.scan(SchemaParserBuilder.kt:169) ~[graphql-java-tools-5.7.1.jar:na]
        at com.coxautodev.graphql.tools.SchemaParserBuilder.build(SchemaParserBuilder.kt:210) ~[graphql-java-tools-5.7.1.jar:na]
        at graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration.schemaParser(GraphQLJavaToolsAutoConfiguration.java:115) ~[graphql-kickstart-spring-boot-autoconfigure-tools-6.0.0.jar:na]
        at graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$a7ca3dd0.CGLIB$schemaParser$3(<generated>) ~[graphql-kickstart-spring-boot-autoconfigure-tools-6.0.0.jar:na]
        at graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$a7ca3dd0$$FastClassBySpringCGLIB$$943c8c05.invoke(<generated>) ~[graphql-kickstart-spring-boot-autoconfigure-tools-6.0.0.jar:na]
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        at graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration$$EnhancerBySpringCGLIB$$a7ca3dd0.schemaParser(<generated>) ~[graphql-kickstart-spring-boot-autoconfigure-tools-6.0.0.jar:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
        ... 142 common frames omitted

Here is my code.

...

type File {
    id: Long!
    userId: Long!
    region: String!
    bucket: String!
    path: String!
    meta: FileMeta!
    createdAt: String!
    updatedAt: String
    user: User!
    url: String!
    thumbs: JSON
}

type FileMeta {
    name: String!
    size: Long!
    type: String!
}

...
...

@Configuration
public class SbipConfig {
    ...

    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper().registerModule(new JavaTimeModule())
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    }

    @Bean
    public SchemaParserOptions schemaParserOptions() {
        return SchemaParserOptions.newOptions()
                .objectMapperProvider(fieldDefinition -> objectMapper()).build();
    }

    @Bean
    public GraphQLScalarType jsonType() {
        return ExtendedScalars.Json;
    }

    @Bean
    public GraphQLScalarType dateTimeType() {
        return ExtendedScalars.DateTime;
    }

    @Bean
    public GraphQLScalarType urlType() {
        return ExtendedScalars.Url;
    }
}
...

@Component
public class FileResolver extends BaseResolver implements GraphQLResolver<FileEntity> {
    @Value("${storage.local.url-base}")
    private String urlBase;

    public UserEntity user(FileEntity fileEntity) {
        return userUsecases.info(fileEntity.getUserId());
    }

    public String url(FileEntity fileEntity) {
        if (fileEntity.getRegion() == FileEntity.Region.LOCAL) {
            return Paths.get(urlBase, fileEntity.getBucket(), fileEntity.getPath()).toString();
        } else {
            return "";
        }
    }

    public Map<FileEntity.ThumbType, String> thumbs(FileEntity fileEntity) {
        if (fileEntity.getMeta().getType().startsWith("image/")) {
            var thumbs = new HashMap<FileEntity.ThumbType, String>();
            var u = url(fileEntity);
            thumbs.put(FileEntity.ThumbType.SMALL,
                    String.format("%s?process=%s", u, "thumb-small"));
            thumbs.put(FileEntity.ThumbType.MIDDLE,
                    String.format("%s?process=%s", u, "thumb-middle"));
            thumbs.put(FileEntity.ThumbType.LARGE,
                    String.format("%s?process=%s", u, "thumb-large"));
            thumbs.put(FileEntity.ThumbType.HUGE, String.format("%s?process=%s", u, "thumb-huge"));
            return thumbs;
        } else {
            return null;
        }
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>net.jaggerwang</groupId>
	<artifactId>spring-boot-in-practice</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Sbip</name>
	<description>Spring boot in practice</description>

	<properties>
		<java.version>11</java.version>
		<start-class>net.jaggerwang.sbip.SbipApplication</start-class>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>

		<dependency>
			<groupId>com.graphql-java-kickstart</groupId>
			<artifactId>graphql-spring-boot-starter</artifactId>
			<version>6.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.graphql-java-kickstart</groupId>
			<artifactId>graphiql-spring-boot-starter</artifactId>
			<version>6.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.graphql-java-kickstart</groupId>
			<artifactId>voyager-spring-boot-starter</artifactId>
			<version>6.0.0</version>
		</dependency>
		<dependency>
			<groupId>com.graphql-java</groupId>
			<artifactId>graphql-java-extended-scalars</artifactId>
			<version>1.0.1</version>
		</dependency>


		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.flywaydb</groupId>
			<artifactId>flyway-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>28.1-jre</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>com.graphql-java-kickstart</groupId>
			<artifactId>graphql-spring-boot-starter-test</artifactId>
			<version>6.0.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
@jaggerwang
Copy link
Author

@oliemansm
Copy link
Member

Have you added scalar JSON to your schema too? Don't see it mentioned here.

@jaggerwang
Copy link
Author

I'm not using this library any more, but directly use GraphQL Java Spring. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants