GraphQL Java 项目教程

GraphQL Java 项目教程

graphql-javaGraphQL Java implementation项目地址:https://gitcode.com/gh_mirrors/gr/graphql-java

1. 项目的目录结构及介绍

GraphQL Java 项目的目录结构如下:

graphql-java/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   ├── graphql/
│   │   │   │   ├── execution/
│   │   │   │   ├── language/
│   │   │   │   ├── schema/
│   │   │   │   ├── validation/
│   │   │   │   └── ...
│   │   └── resources/
│   └── test/
│       ├── java/
│       │   ├── graphql/
│       │   │   ├── execution/
│       │   │   ├── language/
│       │   │   ├── schema/
│       │   │   ├── validation/
│       │   │   └── ...
│       └── resources/
├── pom.xml
├── README.md
└── ...

目录结构介绍

  • src/main/java/graphql/:包含 GraphQL Java 的核心实现代码。
    • execution/:执行 GraphQL 查询的代码。
    • language/:解析 GraphQL 语言的代码。
    • schema/:定义 GraphQL 模式的代码。
    • validation/:验证 GraphQL 查询的代码。
  • src/main/resources/:包含项目的资源文件,如配置文件等。
  • src/test/java/graphql/:包含测试代码。
  • src/test/resources/:包含测试资源文件。
  • pom.xml:Maven 项目的配置文件。
  • README.md:项目说明文档。

2. 项目的启动文件介绍

GraphQL Java 项目的启动文件通常是一个 Java 类,用于启动和配置 GraphQL 服务。以下是一个示例启动文件:

package com.example.graphql;

import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;

import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;

public class GraphQLApplication {

    public static void main(String[] args) {
        String schema = "type Query{hello: String}";

        SchemaParser schemaParser = new SchemaParser();
        TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

        RuntimeWiring runtimeWiring = newRuntimeWiring()
                .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
                .build();

        SchemaGenerator schemaGenerator = new SchemaGenerator();
        GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);

        GraphQL build = GraphQL.newGraphQL(graphQLSchema).build();
        ExecutionResult executionResult = build.execute("{hello}");

        System.out.println(executionResult.getData().toString());
    }
}

启动文件介绍

  • GraphQLApplication:主类,包含 main 方法,用于启动 GraphQL 服务。
  • SchemaParser:解析 GraphQL 模式定义。
  • TypeDefinitionRegistry:存储模式定义。
  • RuntimeWiring:配置运行时数据获取器。
  • SchemaGenerator:生成可执行的 GraphQL 模式。
  • GraphQL:构建和执行 GraphQL 查询。

3. 项目的配置文件介绍

GraphQL Java 项目的配置文件通常是 pom.xml 和一些资源文件。以下是 pom.xml 的示例:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.

graphql-javaGraphQL Java implementation项目地址:https://gitcode.com/gh_mirrors/gr/graphql-java

graphql-java 是 GraphQLJava 实现。这个库的目标是用于真实的生产环境。graphql-java 解析和执行查询 GraphQL 。它并不真正获取任何数据的:数据来源于执行回调或提供静态数据。graphql-java 的 "hello world":import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLObjectType.newObject; public class HelloWorld {     public static void main(String[] args) {         GraphQLObjectType queryType = newObject()                         .name("helloWorldQuery")                         .field(newFieldDefinition()                                 .type(GraphQLString)                                 .name("hello")                                 .staticValue("world")                                 .build())                         .build();         GraphQLSchema schema = GraphQLSchema.newSchema()                         .query(queryType)                         .build();         Map result = new GraphQL(schema).execute("{hello}").getData();         System.out.println(result);         // Prints: {hello=world}     } } 标签:graphql
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郝言元

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值