maven中添加依賴java
<dependency> <groupId>com.graphql-java-kickstart</groupId> <artifactId>graphql-spring-boot-starter</artifactId> <version>5.5.0</version> </dependency> <!-- to embed GraphiQL tool --> <dependency> <groupId>com.graphql-java-kickstart</groupId> <artifactId>graphiql-spring-boot-starter</artifactId> <version>5.5.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.graphql-java-kickstart</groupId> <artifactId>graphql-java-tools</artifactId> <version>5.5.0</version> </dependency>
在resource下面添加schema.graphqlsspring
type Query { post(id: ID): Post } type Post { id: ID name: String }
添加 application.ymlapp
graphql: servlet: mapping: /graphql enabled: true corsEnabled: false //若是spring boot已經配置了cors,則設置關閉 # if you want to @ExceptionHandler annotation for custom GraphQLErrors exception-handlers-enabled: true contextSetting: PER_REQUEST_WITH_INSTRUMENTATION
建立beancors
@Getter @Setter public class Post { private Long id; private String name; Post(Long id){ this.id = id; } }
建立resolvermaven
@Component class Query implements GraphQLQueryResolver { Post getPost(Long id) { return new Post(id); } }
打開
http://localhost:8000/graphiqlspring-boot
{ post(id: 1){ id } }