Java – why is datafetcher not called in this graphql setting?
•
Java
I want to write a piece of code that will handle graphql queries like this:
query { group(id: "com.graphql-java") name(name: "graphql-java") version(id: "2.3.0") }
I created a data extractor and placed a breakpoint in the get method:
import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; public class TestDataFetcher implements DataFetcher { public Object get(final DataFetchingEnvironment dataFetchingEnvironment) { return null; } }
Then I wrote the following code:
public class Example02 { public static void main(final String[] args) throws IOException { final Example02 app = new Example02(); app.run(); } void run() throws IOException { final TestDataFetcher testDataFetcher = new TestDataFetcher(); final List<GraphQLFieldDeFinition> fields = Lists.newArrayList( createGroupField(testDataFetcher),createNameField(),createVersionField()); final GraphQLObjectType queryType = newObject() .name("query") .fields(fields) .build(); final GraphQLSchema schema = GraphQLSchema.newSchema() .query(queryType) .build(); final String query = FileUtils.readFileToString( new File("src/main/resources/query1.txt"),"UTF-8" ); final Map<String,Object> result = (Map<String,Object>) new GraphQL(schema).execute(query).getData(); System.out.println(result); } private GraphQLFieldDeFinition createVersionField() { return newFieldDeFinition().type(GraphQLString).name("version").build(); } private GraphQLFieldDeFinition createNameField() { return newFieldDeFinition().type(GraphQLString).name("name").build(); } private GraphQLFieldDeFinition createGroupField(TestDataFetcher testDataFetcher) { final GraphQLArgument idArg = newArgument().name("id").type(GraphQLString).build(); return newFieldDeFinition() .type(GraphQLString) .name("group") .dataFetcher(testDataFetcher) .argument(idArg) .build(); } }
When I run the main method in debug mode, the breakpoint is not activated
Why? What should I do?
Solution
Here is an example of your work
import graphql.GraphQL; import graphql.schema.*; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLArgument.newArgument; import static graphql.schema.GraphQLFieldDeFinition.newFieldDeFinition; import static graphql.schema.GraphQLObjectType.newObject; public class Example2 { public class TestDataFetcher implements DataFetcher { public Object get(DataFetchingEnvironment environment) { String id = (String)environment.getArgument("id"); return id; } } public static void main(final String[] args) { Example2 app = new Example2(); app.run(); } void run() { TestDataFetcher testDataFetcher = new TestDataFetcher(); List<GraphQLFieldDeFinition> fields = new ArrayList<GraphQLFieldDeFinition>(); fields.add(createGroupField(testDataFetcher)); fields.add(createNameField()); fields.add(createVersionField()); GraphQLObjectType queryType = newObject() .name("query") .fields(fields) .build(); GraphQLSchema schema = GraphQLSchema.newSchema() .query(queryType) .build(); String query = null; try { query = FileUtils.readFileToString( new File("src/main/resources/query1.txt"),"UTF-8" ); }catch(IOException ioe){ ioe.printStackTrace(); } if(query!=null) { Map<String,Object>) new GraphQL(schema).execute(query).getData(); System.out.println(result); } } private GraphQLFieldDeFinition createVersionField() { GraphQLArgument arg = newArgument().name("id").type(GraphQLString).build(); return newFieldDeFinition().type(GraphQLString).name("version").argument(arg).build(); } private GraphQLFieldDeFinition createNameField() { GraphQLArgument arg = newArgument().name("name").type(GraphQLString).build(); return newFieldDeFinition().type(GraphQLString).name("name").argument(arg).build(); } private GraphQLFieldDeFinition createGroupField(TestDataFetcher testDataFetcher) { final GraphQLArgument idArg = newArgument().name("id").type(GraphQLString).build(); return newFieldDeFinition() .type(GraphQLString) .name("group") .dataFetcher(testDataFetcher) .argument(idArg) .build(); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码