Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

java - GraphQl variable using grapiql - variable is undefined

I am using this endpoint:

 @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
    }
}

enter image description here

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

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

enter image description here

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

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

In java code:

@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"));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
  }
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...