使用grapiql的GraphQl变量 - 变量未定义 [英] GraphQl variable using grapiql - variable is undefined

查看:19
本文介绍了使用grapiql的GraphQl变量 - 变量未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个端点:

 @PostMapping("graphql")
    public ResponseEntity<Object> getResource(@RequestBody Object query) { // String query
        ExecutionResult result;
        if (query instanceof String) {
            result = graphQL.execute(query.toString()); // if plain text
        } else{
            String queryString = ((HashMap) query).get("query").toString();
            Object variables = ((HashMap) query).get("variables");
            ExecutionInput input = ExecutionInput.newExecutionInput()
                    .query(queryString)
                    .variables((Map<String, Object>) variables) // "var1" -> "test1"
                    .build();

            result = graphQL.execute(input);
        }
        return new ResponseEntity<Object>(result, HttpStatus.OK);
    }

当我没有变量时它工作正常:

When i don't have variable it works fine:

query {
    getItem(dictionaryType: "test1") {
        code
        name
        description
    }
}

当我添加 variable 时它开始失败,请看这里:

When i add variable it starts to fail, see here:

query {
    getItem(dictionaryType: $var1) {
        code
        name
        description
    }
}

在我的 schema 中,我定义了 query 部分如下:

In my schema i have defined the query section as followed:

type Query {
    getItem(dictionaryType: String): TestEntity
}

java代码中:

@Value("classpath:test.graphqls")
private Resource schemaResource;
private GraphQL graphQL;

@PostConstruct
private void loadSchema() throws IOException {
        File schemaFile = schemaResource.getFile();
        TypeDefinitionRegistry registry = new SchemaParser().parse(schemaFile);
        RuntimeWiring wiring = buildWiring();
        GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(registry, wiring);
        graphQL = GraphQL.newGraphQL(schema).build();
}


private RuntimeWiring buildWiring() {
        initializeFetchers();
        return RuntimeWiring.newRuntimeWiring()
                .type("Query", typeWriting -> typeWriting
                        .dataFetcher("getItem", dictionaryItemFetcher)

                )
                .build();
}

private void initializeFetchers() {
        dictionaryItemFetcher = dataFetchingEnvironment ->
                dictionaryService.getDictionaryItemsFirstAsString(dataFetchingEnvironment.getArgument("dictionaryType"));
}

推荐答案

在操作中使用的任何变量都必须声明为操作定义的一部分,如下所示:

Any variables used inside an operation must be declared as part of the operation definition, like this:

query OptionalButRecommendedQueryName ($var1: String) {
  getItem(dictionaryType: $var1) {
    code
    name
    description
  }
}

这允许 GraphQL 根据提供的类型验证您的变量,并验证是否正在使用变量代替正确的输入.

This allows GraphQL to validate your variables against the provided type, and also validate that the variables are being used in place of the right inputs.

这篇关于使用grapiql的GraphQl变量 - 变量未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆